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.
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.
- Two Hidden variables with name __EVENTTARGET and __EVENTARGUMENT
- A Java script method with name __doPostBack (eventtarget, eventargument)
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.
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.
Following shows how the framework binds the __doPostBack method to the OnChange event.
Was this post helpful to you? How can I improve? – Your comment is highly appreciated!
Cassian Menol Razeek