Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/275.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/asp.net/36.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 从Asp.Net文本框中检索值_C#_Asp.net - Fatal编程技术网

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"> &nbsp; S

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

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

C#代码:

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>

源服务器:
源数据库:
用户名:
密码:
从文本框中检索值的最佳方法是什么

最好的方法是像我现在写的这个控件一样,你有一个按钮,可以让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";
    }
}