简体   繁体   English

编译器错误消息:CS0103:当前上下文中不存在名称“ listBox1”

[英]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.

相关问题 编译器错误消息:CS0103:当前上下文中不存在名称“ProtectedData” - Compiler Error Message: CS0103: The name 'ProtectedData' does not exist in the current context 编译器错误消息:CS0103:名称“绑定”在当前上下文中不存在 - Compiler Error Message: CS0103: The name 'Bind' does not exist in the current context 编译器错误消息:CS0103:在当前上下文中不存在名称“网格” - Compiler Error Message: CS0103: The name 'grid' does not exist in the current context 错误 CS0103:当前上下文中不存在名称“_context” - Error CS0103: The name '_context' does not exist in the current context 错误 CS0103:当前上下文中不存在名称“currentScreen”(CS0103) - Error CS0103: The name 'currentScreen' does not exist in the current context (CS0103) 错误cs0103名称&#39;IEnumerator&#39;在当前上下文中不存在 - error cs0103 The Name 'IEnumerator' does not exist in the current context 错误CS0103:名称“ HttpUtility”在当前上下文中不存在 - error CS0103: The name `HttpUtility' does not exist in the current context "错误 CS0103:当前上下文中不存在名称“AssetPreview”" - error CS0103: The name 'AssetPreview' does not exist in the current context 错误CS0103:名称“ TimeSpan”在当前上下文(CS0103)(testingProgram)中不存在? - Error CS0103: The name 'TimeSpan' does not exist in the current context (CS0103) (testingProgram)? 错误CS0103当前上下文中不存在名称“图像” - Error CS0103 The name 'Image' does not exist in the current context
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM