C# 从Asp.Net文本框中检索值
问题:我有一个asp.net文本框,我正在将文本放入其中,在文本更改时,它应该将其值分配给变量,但不会发生这种情况 问题:从文本框中检索值的最佳方法是什么 C#代码: Asp.Net代码:C# 从Asp.Net文本框中检索值,c#,asp.net,C#,Asp.net,问题:我有一个asp.net文本框,我正在将文本放入其中,在文本更改时,它应该将其值分配给变量,但不会发生这种情况 问题:从文本框中检索值的最佳方法是什么 C#代码: Asp.Net代码: <table style="width: 100%;Border:1px solid Black;"> <tr> <td class="style1"> S
<table style="width: 100%;Border:1px solid Black;">
<tr>
<td class="style1">
Source Server:</td>
<td class="style1">
<asp:TextBox ID="txtSourceServer" runat="server"
ontextchanged="txtSourceServer_TextChanged" AutoPostBack="True"></asp:TextBox>
</td>
<td class="style1">
Source Database:
</td>
<td class="style1">
<asp:TextBox ID="txtSourceDatabase" runat="server"
ontextchanged="txtSourceDatabase_TextChanged" AutoPostBack="True"></asp:TextBox>
</td>
</tr>
<tr>
<td class="style1">
User Name:
</td>
<td class="style1">
<asp:TextBox ID="txtSourceUN" runat="server"
ontextchanged="txtSourceUN_TextChanged" AutoPostBack="True"></asp:TextBox>
</td>
<td class="style1">
Password:
</td>
<td class="style1">
<asp:TextBox ID="txtSourcePass" runat="server"
ontextchanged="txtSourcePass_TextChanged" AutoPostBack="True"></asp:TextBox>
</td>
</tr>
<tr>
<td class="style1">
</td>
<td class="style1">
</td>
<td class="style1">
</td>
<td class="style1">
<asp:Button ID="btnSourceConnect" runat="server" Text="Test Connection"
onclick="btnSourceConnect_Click" />
</td>
</tr>
</table>
源服务器:
源数据库:
用户名:
密码:
从文本框中检索值的最佳方法是什么
最好的方法是像我现在写的这个控件一样,你有一个按钮,可以让post back,然后你可以在code back-on post back上读取文本框的值
在您使用的每个文本框上使用AutoPostBack=“True”
会涉及太多不必要的回发到服务器的内容,最好使用“提交”按钮,并在用户单击提交时保存所有值
在您的情况下,您有一个按钮,您只需要删除“自动回发”并仅在尝试连接时设置您的值:
protected void btnSourceConnect_Click(object sender, EventArgs e)
{
SourceString.UserID = txtSourceUN.Text;
SourceString.DataSource = txtSourceServer.Text;
SourceString.InitialCatalog = txtSourceDatabase.Text;
SourceString.Password = txtSourcePass.Text;
if (Util.Connection(SourceString, 0))
{
lblTesting.Text = "Working!";
}
else
{
lblTesting.Text = "It No Work";
}
}
文本框上的AutoPostback=true将在文本框失去焦点时导致页面发回,而不是在每次文本更改时 从代码的外观来看,大多数功能应该发生在客户端(即使用javascript)
protected void btnSourceConnect_Click(object sender, EventArgs e)
{
SourceString.UserID = txtSourceUN.Text;
SourceString.DataSource = txtSourceServer.Text;
SourceString.InitialCatalog = txtSourceDatabase.Text;
SourceString.Password = txtSourcePass.Text;
if (Util.Connection(SourceString, 0))
{
lblTesting.Text = "Working!";
}
else
{
lblTesting.Text = "It No Work";
}
}