如何从 AJAX Tabcointainer 面板中的文本框中获取值

本文关键字:文本 获取 AJAX Tabcointainer | 更新日期: 2023-09-27 17:57:01

我有一个带有 3 个面板的选项卡容器,每个面板都有一个文本框和一个提交按钮。我想在单击按钮时将文本框的值存储在变量中。我在 fname = txt.text 处收到空值异常;

源:

       <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);
}

如何从 AJAX Tabcointainer 面板中的文本框中获取值

您应该

能够直接引用此控件,因为TabContainerTabPanel都没有实现INamingContainer。因此,TextBox位于页面顶部:

所以而不是

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

fname = this.txtName.Text;