1

问题:我有一个 asp.net 文本框,我正在将文本放入其中,并且在文本更改时它应该将其值分配给一个变量,这不会发生。

问题:从文本框中检索值的最佳方法是什么?

C#代码:

protected void btnSourceConnect_Click(object sender, EventArgs e)
{
    if (Util.Connection(SourceString, 0))
    {
        lblTesting.Text = "Working!";
    }
    else
    {
        lblTesting.Text = "It No Work";
    }
}
protected void txtSourceServer_TextChanged(object sender, EventArgs e)
{
        SourceString.DataSource = txtSourceServer.Text;      
}

protected void txtSourceDatabase_TextChanged(object sender, EventArgs e)
{

        SourceString.InitialCatalog = txtSourceDatabase.Text;

}

protected void txtSourceUN_TextChanged(object sender, EventArgs e)
{

        SourceString.UserID = txtSourceUN.Text;

}

protected void txtSourcePass_TextChanged(object sender, EventArgs e)
{

        SourceString.Password = txtSourcePass.Text;   
}

ASP.NET 代码:

<table style="width: 100%;Border:1px solid Black;">
    <tr>
            <td class="style1">
                &nbsp;
                Source Server:</td>
            <td class="style1">
                &nbsp;&nbsp;
                <asp:TextBox ID="txtSourceServer" runat="server" 
                    ontextchanged="txtSourceServer_TextChanged" AutoPostBack="True"></asp:TextBox>
            </td>
            <td class="style1">
                &nbsp; Source Database:
            </td>
            <td class="style1">
                &nbsp;&nbsp;
                <asp:TextBox ID="txtSourceDatabase" runat="server" 
                    ontextchanged="txtSourceDatabase_TextChanged" AutoPostBack="True"></asp:TextBox>
            </td>

        </tr>
        <tr>
            <td class="style1">
                &nbsp; User Name:
            </td>
            <td class="style1">
                &nbsp;&nbsp;
                <asp:TextBox ID="txtSourceUN" runat="server" 
                    ontextchanged="txtSourceUN_TextChanged" AutoPostBack="True"></asp:TextBox>
            </td>
            <td class="style1">
                &nbsp; Password:
            </td>
            <td class="style1">
                &nbsp;&nbsp;
                <asp:TextBox ID="txtSourcePass" runat="server" 
                    ontextchanged="txtSourcePass_TextChanged" AutoPostBack="True"></asp:TextBox>
            </td>
        </tr>
        <tr>
            <td class="style1">
                &nbsp;
            </td>
            <td class="style1">
                &nbsp;
            </td>
            <td class="style1">
                &nbsp;
            </td>
            <td class="style1">
                &nbsp;
                <asp:Button ID="btnSourceConnect" runat="server" Text="Test Connection" 
                    onclick="btnSourceConnect_Click" />
            </td>
        </tr>
        </table>
4

2 回答 2

1

从文本框中检索值的最佳方法是什么?

最好的方法就像我现在正在写的这个控件,你有一个回发按钮,然后你在后面的代码中读取文本框的值 - 在回发上。

在您使用的AutoPostBack="True"每个 TextBox 上使用都涉及到太多不必要的回发到服务器,最好使用“提交”按钮并在用户单击提交时保存所有值。

在您的情况下,您有一个按钮,您只需要在尝试连接时删除自动回发并设置您的值:

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";
    }
}
于 2012-11-19T18:57:36.183 回答
1

文本框上的 AutoPostback = true 将导致页面在文本框失去焦点时回发,而不是在每次文本更改时回发。

从您的代码的外观来看,大部分功能应该发生在客户端(即使用 javascript)。

于 2012-11-19T18:59:18.260 回答