UncleCoder.com

UncleCoder.com

Free programming examples and instructions

Understanding Autopostback property

Demo and instrunctions for how autopostbacks works in asp.net

by Athil


Posted on 06 Mar 2017 Category: Asp.net Views: 824

Edited on 02 Jun 2017



Hi, Here I am going to explain why Autopostback property is used for, some controls Like Textbox, Radiobutton Checkbox etc have Autopostback property. Before that you need to understand what is a postback.

 

DEMO

A Postback is a complete request to a server and give a response to client side. Most of the controls will automatically postback, Eg- A button with click event, automatically post back when you click on a button. But in the case of Textbox It will not happen until you changed the Autopostback property to true.  the post back requests are collected together and sent to the server along with a post back,

For example you have a text box and a button on a form. You give a TextChanged  event to text box, You add some codes on there, at the time of running the event will not fire. When you click on the button the textChanged event will fire, This is actually TextChanged Event but this postback request was send along with the buttons' postback. 

When you change the property Autopostback = true to the TextBox the event will fire at the time of text changed and click on outside of the control.

Why Autopostback

You may catch confused about Autopostback and why it is for. Imagine that you have a form with lots of checkboxes and  Radiobuttons about your personal details. When we enable Autopostback = true for each control and each controls CheckedChanged event has some code, The page will refresh at each time when we check a control, When we make it false then postback requests are collected and send along with a post back. So, In this case these controls not required Autopostback to true. The postback request will send along with submit buttons postback request.

Through Example

On Client Side

   <form id="form1" runat="server">
    <div>
    
        <strong>TextBox Without Autopostback<br />
        <asp:TextBox ID="TextBox1" runat="server" OnTextChanged="TextBox1_TextChanged"></asp:TextBox>
        </strong>
    
    </div>
        <asp:Button ID="Button1" runat="server" Text="Click Here after typing" />
        <p>
            <strong>TextBox With Autopostback</strong></p>
        <p>
            <asp:TextBox ID="TextBox2" runat="server" AutoPostBack="True" OnTextChanged="TextBox2_TextChanged"></asp:TextBox>
        </p>
        <p>
            &nbsp;</p>
        <p>
            <strong>CheckBoxes Without Autopostback</strong></p>
       
            <asp:CheckBox ID="CheckBox1" runat="server" OnCheckedChanged="CheckBox1_CheckedChanged" Text="CB1" />
        
        <br />
        
        <asp:CheckBox ID="CheckBox2" runat="server" OnCheckedChanged="CheckBox2_CheckedChanged" Text="CB2" />
        <br />
        <asp:CheckBox ID="CheckBox3" runat="server" OnCheckedChanged="CheckBox3_CheckedChanged" Text="CB3" />
        <br />
        <asp:Button ID="Button2" runat="server" Text="Click Here After checking some check boxes" />
        <br />
        <br />
        <br />
        <strong>ChecckBoxes With AutopostBack</strong><br />
        <asp:CheckBox ID="CheckBox4" runat="server" AutoPostBack="True" OnCheckedChanged="CheckBox4_CheckedChanged" Text="CB4" />
        <br />
        <asp:CheckBox ID="CheckBox5" runat="server" OnCheckedChanged="CheckBox5_CheckedChanged" Text="CB5" />
        <br />
        <asp:CheckBox ID="CheckBox6" runat="server" OnCheckedChanged="CheckBox6_CheckedChanged" Text="CB6" />
    </form>

 

On Server Side

 

protected void TextBox1_TextChanged(object sender, EventArgs e)
        {
            Response.Write(TextBox1.Text+"<br>");
        }

        protected void TextBox2_TextChanged(object sender, EventArgs e)
        {Response.Write(TextBox2.Text + "<br>");
        }

        protected void CheckBox1_CheckedChanged(object sender, EventArgs e)
        {if (CheckBox1.Checked == true)
                Response.Write("Checked Checkbox1 <br>" );
            else
                Response.Write("Not checked CheckBox1 <br>");

        }

        protected void CheckBox4_CheckedChanged(object sender, EventArgs e)
        {if (CheckBox1.Checked == true)
                Response.Write("Checked Checkbox4 <br>");
            else
                Response.Write("Not checked CheckBox4 <br>");
        }

        protected void CheckBox2_CheckedChanged(object sender, EventArgs e)
        {if (CheckBox2.Checked == true)
                Response.Write("Checked Checkbox2 <br>");
            else
                Response.Write("Not checked CheckBox2 <br>");
        }

        protected void CheckBox3_CheckedChanged(object sender, EventArgs e)
        { if (CheckBox3.Checked == true)
                Response.Write("Checked Checkbox3 <br>");
            else
                Response.Write("Not checked CheckBox3 <br>");
        }

        protected void CheckBox5_CheckedChanged(object sender, EventArgs e)
        { if (CheckBox5.Checked == true)
                Response.Write("Checked Checkbox5 <br>");
            else
                Response.Write("Not checked CheckBox5 <br>");
        }

        protected void CheckBox6_CheckedChanged(object sender, EventArgs e)
        {if (CheckBox6.Checked == true)
                Response.Write("Checked Checkbox6 <br>");
            else
                Response.Write("Not checked CheckBox6 <br>");
        }

 



Leave a Comment:


Advertisement