TextBox AutoPostBack and TextChanged Event ASP.Net

TextBox AutoPostBack ASP.Net Example

In this asp.net tutorial we will learn about TextBox control TextChanged events and AutoPostBack property with an example.

TextBox TextChanged event and AutoPostBack in ASP.Net
TextBox TextChanged event and AutoPostBack in ASP.Net

TextBox control is a input control in asp.net, that allow user to input text on web page. Here we will disccuss TextBox TextChanged event in asp.net. The TextBox TextChanged event occurs when text changed in textbox control. The TextChanged event triggers while text changed in textbox.

  • Note : Set TextBox property AutoPostBack = True

TextChanged TextBox ASP.Net Example

Generally we write c# code in button click events, so buttons AutoPostBack property by default set true.
If we write some c# code on TextBox TextChanged event and want to execute the code, we must first set AutoPostBack = True of TextBox control.

Here, In below example we will do sum of two integer using textbox TextChanged event.

Step 1 : Open Visual Studio — > Create Empty Website

Step 2 : Add New WebForm

Step 3 : Design Web form with Two TextBox Control along with one Label control

Step 4  : Set AutoPostBack = True for TextBox2 control

Step 5 : write C# code on TextChange event of TextBox2 control

TextBox AutoPostBack ASP.Net Example

HTML Design code of .aspx page

<form id="form1" runat="server">
<div>    
<table>
  <tr>
    <td colspan="2">OnTextChanged and AutoPostBack</td>
  </tr>           
  <tr>
    <td class="lbl">Enter Value 1 : </td>
    <td>
      <asp:TextBox ID="TextBox1" runat="server" ></asp:TextBox></td> </tr>
  <tr>
    <td class="lbl">Enter Value 2 :</td>
    <td>
     <asp:TextBox ID="TextBox2" runat="server" AutoPostBack="True" OnTextChanged="TextBox2_TextChanged"></asp:TextBox>
    </td> </tr>
  <tr>
    <td>&nbsp;</td>
    <td>
     <asp:Label ID="lblsum" runat="server" ></asp:Label> </td> </tr>            
 </table>    
</div>
</form>
TextBox TextChanged event and AutoPostBack in ASP.Net
TextBox TextChanged event and AutoPostBack in ASP.Net

C# Code for above TextBox Example

protected void Page_Load(object sender, EventArgs e)
    {

    }
    protected void TextBox2_TextChanged(object sender, EventArgs e)
    {
        int sum = Convert.ToInt32(TextBox1.Text) + Convert.ToInt32(TextBox2.Text);
        lblsum.Text = "The Sum = " +  sum.ToString();
        TextBox1.Text = "";
       TextBox2.Text = "";
    }

4 thoughts on “TextBox AutoPostBack and TextChanged Event ASP.Net

  1. Thank you…Thank you…Thank you! This is the first site (after viewing dozens) that used ‘AutoPostBack’. I have been pulling my hair and cussing for the past week trying to get the ‘OnTextChanged’ to fire and after adding ‘AutpPostBack=”True'” to my textbox, it finally did .

    I am a happy man.

  2. Hi, thanks for your comment, if we also need to make the first textbox make changes in label value. to get to input data change.how please.

Leave a Reply

Your email address will not be published. Required fields are marked *