Automatic and Postponed PostBacks

Web Controls like the Button or LinkButton automatically PostBack when Clicked, other Web Controls like the TextBox have the ability to do Postponded PostBacks as well as Automatic PostBacks. A Postponed PostBack is when the PostBack Event is only happens when another Web Control does a PostBack. The Postponded PostBack event handler method fires before the event handlers for the sending Web Control.

In this example we will create a simple page with two TextBoxes and two Buttons

 

    

Postponed PostBack

Automatic PostBack

Output

Then lets hook up the event handlers for the Buttons and the TextBoxes

 

        protected void Page_Load(object sender, EventArgs e)
        {
            myButton1.Click += MyButton1Click;
            myButton2.Click += MyButton2Click;
            myTextBox1.TextChanged += MyTextBox1TextChanged;
            myTextBox2.TextChanged += MyTextBox2TextChanged;
        }

        private void MyTextBox1TextChanged(object sender, EventArgs e)
        {
            ltOutput.Text += string.Format("MyTextBox1TextChanged - myTextBox1: {0}
", myTextBox1.Text); } private void MyTextBox2TextChanged(object sender, EventArgs e) { ltOutput.Text += string.Format("MyTextBox2TextChanged - myTextBox2: {0}
", myTextBox2.Text); } private void MyButton2Click(object sender, EventArgs e) { ltOutput.Text += string.Format("MyButton2Click - myTextBox2: {0}
", myTextBox2.Text); } private void MyButton1Click(object sender, EventArgs e) { ltOutput.Text += string.Format("MyButton1Click - myTextBox1: {0}
", myTextBox1.Text); }

You will find that when myTextBox2 loses focus it does a PostBack of its own, but myTextBox1 does not. But if you change the value in myTextBox1 and then PostBack with a Button both event handlers fire.

You can download the Automatic and Postponed PostBacks project here.



Comments

No comments yet.

Add Yours

  • Author Avatar

    YOU


Comment Arrow




About Author

Robert

Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the Universe trying to produce bigger and better idiots. So far, the Universe is winning hands down.