2013-03-08 258 views
0

我得到这个错误:the name 'textbox1' does not exist in the current context名称“TextBox1中”不存在于当前上下文存在

我的代码:

namespace WebApplication19 
{ 
    public partial class Default:System.Web.UI.Page 

    { 
     protected void TextBox1_TextChanged(object sender, EventArgs e) 
     { 
      if (TextBox1.Text == "msc") 
      { 
       RadioButton2.Visible = false; 
       DropDownList2.Visible = false; 
      } 
      else 
      { 
       RadioButton1.Visible = false; 
       DropDownList1.Visible = false; 
      }    
     } 
    } 
} 
+4

是否有'TextBox1'在'.aspx'?如果_was_和你重命名了,那么你应该在上面的代码中重命名它。 – Oded 2013-03-08 16:20:09

+1

添加到@Oded所说的内容中,检查该文本框是否也在* .designer.cs *文件中 – DGibbs 2013-03-08 16:24:49

+0

y TextBox1仅在designer.cs文件中存在,ID仅在“TextBox1”中。 – 2013-03-08 16:34:38

回答

0

添加runat="server"TextBox1标记,并确保它具有ID="TextBox1以及。

1

除了@Michael和@DGibbs给出的建议外,我还建议你在所有文件(ASPX,ASPX.cs和ASPX.Designer.cs)中检查类名,以确保它们是双重和三重的。它们匹配,以及名称空间声明。另外,除非这只是一个概念的快速和肮脏的证明,否则您应该为您的命名空间提供一个更具描述性的名称。除“WebApplication19”之外的其他内容。

最后,很明显错误消息明确指出:the name 'textbox1' does not exist in the current context而不是TextBox1。搜索您的代码小写textbox1并将其重命名为匹配正确的大写名称,并且应该让您的代码进行编译。

示例标记:

<asp:TextBox ID="TextBox1" runat="server" ontextchanged="TextBox1_TextChanged"></asp:TextBox> 
+0

我的问题解决了,非常感谢你的朋友 – 2013-03-09 05:25:24

相关问题