[英]Compiler Error Message: CS0103: The name 'listBox1' does not exist in the current context
I am getting the above error. 我收到上述错误。 The error is on line 33, it says. 它说,错误在第33行。
Here's the code: 这是代码:
Source Error: 源错误:
Line 31:
Line 32: while(r.Read()) {
Line 33: listBox1.Items.Add(new ListItem(r["first_name"], r["first_name"])); //this is the offending line
Line 34: }
Line 35: con.Close();
Now, I figured my ASP.NET was fine, I have the following code there: 现在,我发现我的ASP.NET很好,那里有以下代码:
<asp:ListBox ID="listBox1" runat="server">
</asp:ListBox>
What might I be doing incorrectly here? 我在这里可能做错了什么? I have the user control setup in the .aspx file, I have the correct item in my .aspx.cs file - it should work, shouldn't it? 我在.aspx文件中有用户控件设置,在.aspx.cs文件中有正确的项目-它应该可以工作,不是吗? What am I missing? 我想念什么? I'm still new to C# 我还是C#的新手
If you have created a UserControl
instance inside your aspx then you have to find the ListBox
control inside the UserControl
instance using its Controls
collection. 如果您在aspx中创建了UserControl
实例,则必须使用UserControl
实例的Controls
集合在UserControl
找到ListBox
Controls
。
Here's a link on MSDN. 这是MSDN上的链接。
Another alternative is to have your individual control handlers inside the code-behind file of the UserControl
. 另一种替代方法是将您自己的控件处理程序放在UserControl
的代码隐藏文件内。
Does your listBox1 control exist inside your designer.cs file? 您的listBox1控件是否存在于designer.cs文件中? If not, regenerate the auto-generated C# file by deleting it, right clicking the aspx file and clicking on "Convert to Web Application". 如果不是,请删除自动生成的C#文件,右键单击aspx文件,然后单击“转换为Web应用程序”,以重新生成该文件。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.