2013-01-31 130 views
0

我有一個tabcontainer 3面板,每個人都有幾個文本框和一個提交按鈕。我想將文本框的值存儲在按鈕單擊的變量中。我在fname = txt.text處得到空值異常;如何從AJAX Tabcointainer面板中的文本框中獲取值?

來源:

 <body> 
    <form id="form1" runat="server"> 
    <asp:ToolkitScriptManager ID="ToolkitScriptManager1" runat="server"> 
     </asp:ToolkitScriptManager> 
    <div> 

     <asp:TabContainer ID="TabContainer1" runat="server" ActiveTabIndex="0" 
      style="margin-right: 38px" Height="138px" Width="402px"> 
      <asp:TabPanel runat="server" HeaderText="TabPanel1" ID="TabPanel1"> 

       <ContentTemplate> 
<asp:UpdatePanel ID="updatePanel1" runat="server"><ContentTemplate><table><tr><td>First Name:</td><td><asp:TextBox ID="txtName" runat="server" /></td></tr><tr><td>Address:</td><td><asp:TextBox ID="txtAddress" runat="server" /></td></tr><tr><td><asp:Button ID="Button1" runat="server" Text="Submit" 
             onclick="Button1_Click1" /></td></tr></table></ContentTemplate></asp:UpdatePanel> 
</ContentTemplate> 

</asp:TabPanel> 

      <asp:TabPanel ID="TabPanel2" runat="server" HeaderText="TabPanel2"> 
      <ContentTemplate> 
        Email: <asp:TextBox ID="txtEmail" runat="server" /> 


</ContentTemplate> 

</asp:TabPanel> 
      <asp:TabPanel ID="TabPanel3" runat="server" HeaderText="TabPanel3"> 
      <ContentTemplate> 
       <table> 
       <tr> <td>User Name:</td><td><asp:TextBox ID="txtUser" runat="server" /></td></tr> 
       <tr> <td>Password:</td><td><asp:TextBox ID="txtPass" runat="server" /></td></tr> 

</ContentTemplate> 

</asp:TabPanel> 
     </asp:TabContainer> 

    </div> 
    </form> 
</body> 

C# Code: 



protected void Button1_Click1(object sender, EventArgs e) 
{ 
    string fname = ""; 
    string address = ""; 
    Button btn = (Button)sender; 

    TextBox txt = null; 
    TextBox txt1 = null; 


    txt = (TextBox)TabContainer1.Parent.FindControl("txtName"); 
    txt1 = (TextBox)TabContainer1.Parent.FindControl("txtAddress"); 
    fname = txt.Text; 
    address = txt1.Text; 
    DataTable dt = new DataTable(); 
    string q = "insert into tabtable (firstname,address) values('" + txt + "','" + txt1 + "')"; 
    dboperation dbo = new dboperation(); 
    dt = dbo.insert(q); 
} 

回答

1

你應該能夠因爲無論TabContainer也不TabPanel工具INamingContainer直接引用該控件。因此,TextBox是在頁面的頂部:

所以不是

txt = (TextBox)TabContainer1.Parent.FindControl("txtName"); 
fname = txt.Text; 

只是

fname = this.txtName.Text; 
+0

該死!我無法相信我花了3個小時在這樣愚蠢的事情上。謝謝 !! :) – Arbaaz

+0

順便說一句你能解釋爲什麼我必須使用'這'? – Arbaaz

+0

@ user1856304:你不需要使用'this'。它應該只是強調我的話,它在頁面頂部,而不是在不同的NamingContainer中。 –

相關問題