2013-03-08 194 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

相關問題