ASP.NET – AutoPostBack : What is AutoPostBack and How AutoPostBack Works

2008-12-26

Today I was experimenting on a grid view where I was trying to select multiple rows of the grid view using a check box column.

I wrote some code in the CheckedChanged of checkbox but then I found that the code was not executed when the state of the checkbox is changed.

So I did a little googling and found out about this AutoPostBack property. This property defines whether the control should post back to the server each time when the user interacts with the control. Or, according to this scenario, a post back will fire when the user clicks on the check box or when the Checked property is changed.

AutoPostBack :

This value holds a boolean value (true/false)

If the property is set to true, a post back is sent immediately to the server and no post back is occurred when set to false.

The Use of AutoPostBack:

According to MSDN, for most WebControls, when AutoPostBack is false, only the events from actions that cause a net change in the state of the control are submitted to the server.

In other words some events are not queued to the server. For example no event is fired when a user selects a value from a drop down list or when user presses Enter or Tab key after entering a value to a textbox.

If you want such events to be fired then you have to enable autopostback by setting autopostback property to true.

How AutoPostBack Works :

When AutoPostBack is enabled, the .Net framework automatically injects following additional items into the generated HTML code.

  1. Two Hidden variables with name __EVENTTARGET and __EVENTARGUMENT
  2. A Java script method with name __doPostBack (eventtarget, eventargument)
  3. OnChange JavaScript event to the control

What is __EVENTTARGET :

__EVENTTARGET tells the server which control wants to fire the event so that the framework can fire the event on that control.

What is __EVENTARGUMENT :

__EVENTARGUMENT can be used to provide additional information to the server about the event.

What is __doPostBack (eventtarget, eventargument) :

Parameters sent to this method holds relevant target and event argument values and this method sets those values into __EVENTTARGET and __EVENTARGUMENT hidden variables so that the server can read those.

Then this method submits the form to the server where the appropriate event will be fired.

What is OnChange JavaScript event to the control :

Every control has a client side event called OnChange. When AutoPostBack is enabled for a control the framework sets the handler for this client side event as the __doPostBack method and will pass the name of the control as the first parameter, eventtarget.

Ex/

Following shows how the framework binds the __doPostBack method to the OnChange event.

<input type=”checkbox” onclick=“javascript:setTimeout(‘__doPostBack(‘CheckBox1’,”)‘, 0)” />

Was this post helpful to you? How can I improve? – Your comment is highly appreciated!

Cassian Menol Razeek

Recommended Books:

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: