66Mhz
66Mhz

Reputation: 225

How to update textbox onchange without postback?

Is there a way to update a textbox without using postback? I'd like to populate the txtDFS field with the value of (100 - txtStocked) after the user enters data in the txtStocked field.

The following code works, but it uses postback so the page refreshes. Is there a way to accomplish this without refreshing the page?

<asp:TextBox ID="txtStocked" runat="server" Width="75px" AutoPostBack="true" OnTextChanged="txtStocked_TextChanged"></asp:TextBox>
<asp:TextBox ID="txtDFS" runat="server" Width="75px"></asp:TextBox>

protected void txtStocked_TextChanged(object sender, EventArgs e)
    {
        int stocked = Convert.ToInt16(txtStocked.Text);
        int dfs = (100 - stocked);
        txtDFS.Text = dfs.ToString();
        txtDFS.Text = txtStocked.Text;
    }

Upvotes: 0

Views: 11684

Answers (1)

66Mhz
66Mhz

Reputation: 225

Solved this by using an UpdatePanel. Here is the updated code:

<asp:ScriptManager ID="SCManager" runat="server" />
<asp:UpdatePanel ID="SCPanel" runat="server" UpdateMode="Conditional" ChildrenAsTriggers="false">
<ContentTemplate>

<asp:TextBox ID="txtStocked" runat="server" Width="75px" autopostback="true" OnTextChanged="Update_Text"></asp:TextBox>
<asp:TextBox ID="txtDFS" runat="server" Width="75px" ReadOnly="True"></asp:TextBox>

</ContentTemplate>
</asp:UpdatePanel>


protected void Update_Text(object sender, EventArgs e)
    {            
        int stocked = Convert.ToInt16(txtStocked.Text);
        int dfs = (100 - stocked);
        txtDFS.Text = dfs.ToString();            
        SCPanel.Update();
    }

Upvotes: 1

Related Questions