Can't figure out the error:does not exist in the current context
-
Hello everybody, I am getting an error: The name 'DataList1' does not exist in the current context. DataList1 is a data list the code is in two pages: Have no idea what the problem is. Any assistance is appreciated. groups.aspx, and groups.aspx.cs groups.aspx: I remobed the open tags so that the code can be seen. asp:DataList ID="DataList1" runat="server" DataSourceID="ChalkDL"> ItemTemplate> asp:TextBox ID="postComment" runat="server"> asp:Button runat="server" OnClick="postBtn_Click" ID="postBtn" Text="Post!" /> /ItemTemplate> /asp:DataList> groups.aspx.cs protected void postBtn_Click(object sender, EventArgs e) { SqlDataSource PostDS = new SqlDataSource(); System.Web.UI.WebControls.TextBox PostText = (System.Web.UI.WebControls.TextBox)DataList1.FindControl("postComment"); PostDS.ConnectionString = ConfigurationManager.ConnectionStrings["ConnectionString"].ToString(); PostDS.InsertCommandType = SqlDataSourceCommandType.Text; PostDS.InsertCommand = "INSERT INTO GroupChalkboard (Text, GroupID, UserID, CreatedDate) VALUES (@Text, @GroupID, @UserID, @CreatedDate)"; PostDS.InsertParameters.Add("Text", PostText.Text); PostDS.InsertParameters.Add("GroupID", pid.ToString()); PostDS.InsertParameters.Add("UserID", Session["UserID"].ToString()); PostDS.InsertParameters.Add("CreatedDate", DateTime.Now.ToString()); PostDS.Insert(); }
-
Hello everybody, I am getting an error: The name 'DataList1' does not exist in the current context. DataList1 is a data list the code is in two pages: Have no idea what the problem is. Any assistance is appreciated. groups.aspx, and groups.aspx.cs groups.aspx: I remobed the open tags so that the code can be seen. asp:DataList ID="DataList1" runat="server" DataSourceID="ChalkDL"> ItemTemplate> asp:TextBox ID="postComment" runat="server"> asp:Button runat="server" OnClick="postBtn_Click" ID="postBtn" Text="Post!" /> /ItemTemplate> /asp:DataList> groups.aspx.cs protected void postBtn_Click(object sender, EventArgs e) { SqlDataSource PostDS = new SqlDataSource(); System.Web.UI.WebControls.TextBox PostText = (System.Web.UI.WebControls.TextBox)DataList1.FindControl("postComment"); PostDS.ConnectionString = ConfigurationManager.ConnectionStrings["ConnectionString"].ToString(); PostDS.InsertCommandType = SqlDataSourceCommandType.Text; PostDS.InsertCommand = "INSERT INTO GroupChalkboard (Text, GroupID, UserID, CreatedDate) VALUES (@Text, @GroupID, @UserID, @CreatedDate)"; PostDS.InsertParameters.Add("Text", PostText.Text); PostDS.InsertParameters.Add("GroupID", pid.ToString()); PostDS.InsertParameters.Add("UserID", Session["UserID"].ToString()); PostDS.InsertParameters.Add("CreatedDate", DateTime.Now.ToString()); PostDS.Insert(); }
Albert83 wrote:
System.Web.UI.WebControls.TextBox PostText = (System.Web.UI.WebControls.TextBox)DataList1.FindControl("postComment");
This line should be
TextBox PostText=(TextBox)DataList1.Items[IndexHere].FindControl("PostComment");
Albert83 wrote:
I am getting an error: The name 'DataList1' does not exist in the current context.
Can you get DataList1 control in codebehind side? If no, It should be inherit problem of the page. Check page directory of groups.aspx and class name of groups.aspx.cs.
please don't forget to vote on the post that helped you.
-
Albert83 wrote:
System.Web.UI.WebControls.TextBox PostText = (System.Web.UI.WebControls.TextBox)DataList1.FindControl("postComment");
This line should be
TextBox PostText=(TextBox)DataList1.Items[IndexHere].FindControl("PostComment");
Albert83 wrote:
I am getting an error: The name 'DataList1' does not exist in the current context.
Can you get DataList1 control in codebehind side? If no, It should be inherit problem of the page. Check page directory of groups.aspx and class name of groups.aspx.cs.
please don't forget to vote on the post that helped you.
I can't get the DataList in code behind that's the problem. The weird thing DataList1 does show up in the list and I can choose Items property and find control property. But when I run it, it says DataList1 does not exist in the current context. How to resolve it ? That's aspx: %@ Page Language="C#" AutoEventWireup="true" CodeFile="groups.aspx.cs" Inherits="groups" %> that's aspx.cs public partial class groups : System.Web.UI.Page { Also In this code: TextBox PostText=(TextBox)DataList1.Items[IndexHere].FindControl("PostComment"); what's Items[IndexHere] - what index is that. Should I count the number of items in the ItemTemplate?
-
I can't get the DataList in code behind that's the problem. The weird thing DataList1 does show up in the list and I can choose Items property and find control property. But when I run it, it says DataList1 does not exist in the current context. How to resolve it ? That's aspx: %@ Page Language="C#" AutoEventWireup="true" CodeFile="groups.aspx.cs" Inherits="groups" %> that's aspx.cs public partial class groups : System.Web.UI.Page { Also In this code: TextBox PostText=(TextBox)DataList1.Items[IndexHere].FindControl("PostComment"); what's Items[IndexHere] - what index is that. Should I count the number of items in the ItemTemplate?
Albert83 wrote:
The weird thing DataList1 does show up in the list and I can choose Items property and find control property.
Did you get runtime error or compile time error?
Albert83 wrote:
In this code: TextBox PostText=(TextBox)DataList1.Items[IndexHere].FindControl("PostComment"); what's Items[IndexHere] - what index is that. Should I count the number of items in the ItemTemplate?
Yes.you have to do looping through datalist items.
please don't forget to vote on the post that helped you.
-
Albert83 wrote:
The weird thing DataList1 does show up in the list and I can choose Items property and find control property.
Did you get runtime error or compile time error?
Albert83 wrote:
In this code: TextBox PostText=(TextBox)DataList1.Items[IndexHere].FindControl("PostComment"); what's Items[IndexHere] - what index is that. Should I count the number of items in the ItemTemplate?
Yes.you have to do looping through datalist items.
please don't forget to vote on the post that helped you.