简体   繁体   中英

Retrieving value from Asp.Net TextBox

Problem: I have an asp.net Textbox that I am putting text into and on text change it should assign its value to a variable, this does not happens.

Question: What is the best method to retrieve the value from the Textbox?

C# Code:

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 Code:

<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>

What is the best method to retrieve the value from the Textbox?

The best method is like this control I am writing right now, you have a button that make post back, and then you read the value of the text box on code behind - on post back.

Using the AutoPostBack="True" on every TextBox you use involves too many unnecessary post backs to the server, it is better to use a "submit" button and save all your values when the user click on submit.

In you case you have a button, and you only need to remove the auto post back and set your values only when you try to connect as:

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 on a TextBox will cause the page to post back when the text box loses focus, not on every text change.

From the look of your code most of this functionality should be happening on the client side (ie with javascript).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM