我有一个带有 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);
}