Asp.NET Page Life Cycle - Raise Postback Event

Lawrence Avery
Lawrence Avery used Ask the Experts™
on
My understanding there are two flavors of postback events.

1) The first is a changed event. The event fires when some piece of data is changed between postbacks. Example is a DropLists - SelectedIndexChange event or the Textbox's textChanged event.

2) The other flavor of postback is the raised event. An example is the button click.


Tell  me if my understanding is right for the changed event - the postback data is compared to the viewstate data to cause a change event to fire.

How does raised event - like the button click get recogonized on the server side?
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Miguel OzSenior Software Engineer
Top Expert 2009

Commented:
1) Change event firing decision is done in browser (Control is mapped to Html select that supports onchange event).
2) Button typically submits the form to the server.
In general the server receives any post back browser request and it decides whether the request is done by a particular analyzing the request parameters.
You can check it your yourself by adding the following code to your page load event:
string eventTarget = Request.Params.Get("__EVENTTARGET");

Open in new window

eventTarget will contain your target button or control id in general
Lawrence AverySystem Developer

Author

Commented:
You're saying in regards to Button click, the button and eventhandler name  will added to eventTarget so server will execute the correct event handler?

In regards to Change event, will the control id and its eventhandler name be assigned to eventTarget also?
Lawrence AverySystem Developer

Author

Commented:
I guess what I am saying is how does the client side indicate -- for instance say --a  dropdownlist control changed it selection and also how does the client tell the server what method to call on selection change?
These are nonpostback events.
CompTIA Security+

Learn the essential functions of CompTIA Security+, which establishes the core knowledge required of any cybersecurity role and leads professionals into intermediate-level cybersecurity jobs.

Miguel OzSenior Software Engineer
Top Expert 2009

Commented:
The client side sends the information as part of the Request. This is already done by the code generated by the server (JavaScript) and totally transparent to you. Just do a view source or use IE developer tools (press F12 - DOM tab)
For example, in the dropdown list example, this is mapped to a HTML select  element and ASP.NET generates the onchange event that contains a call to submit the page (form) to server. Then the server checks the event target and argument and calls page_load and the appropriate control event.
Lawrence AverySystem Developer

Author

Commented:
I understand the event target and argument  is the form variable that holds the name of the control which issued the submit.   But what about events on the page that do not issue the submit.

I am saying on the aspx page we
1st)     select a item in dropdownlist which doesn't submit the page
2nd)  then subsequently hit a button to submit the form.

So in this case event target is the submit button not the dropdownlist. So how does the aspx page on server side know that it must process also the change event when selecting an item in the dropdownlist?

To me there are 2 events here the click event and the change event. How does the server know about  both events  when event targets holds only one event?  At least is what I think it does.

See this and read the bottom of it.
http://devproconnections.com/aspnet/detect-control-changes
Miguel OzSenior Software Engineer
Top Expert 2009

Commented:
Your case is different because the dropdown list is not posting back to server. Even though the button submits the page, all OTHER controls are recreated in the server either by using view state or using the current selected option if view state is false.
From client  (browser) there are two events (change and click), from server perspective there is one event (button click).
Lawrence AverySystem Developer

Author

Commented:
I don't see how from server perspective there is one event(button click) when the server must execute an event handler for the change event.  I am going to research a little more and get back to this in a couple of days.  Under  Understanding ASP.NET View State  msdn article - Stage 5 - Raise Postback Event. in particular it says raise postback event stage does not use the View State.
So how does it do a comparison of old value to new value on change event?

See link
https://msdn.microsoft.com/en-us/library/ms972976.aspx
Lawrence AverySystem Developer

Author

Commented:
I looked at the interface IPostBackDataHandler and indicates it uses the ViewState -
see
https://msdn.microsoft.com/en-us/library/system.web.ui.ipostbackdatahandler(v=vs.110).aspx

now I am confused.   because if you look article:
https://msdn.microsoft.com/en-us/library/ms972976.aspx 

read Stagee 5 section  - Raise Postback Event - last paragragh-
It says:
Since this stage inspects postback data to determine if any events need to be raised, the stage only occurs when the page has been posted back. As with the load postback data stage, the raise postback event stage does not use view state information at all. Whether or not an event is raised depends on the data posted back in the form fields.
Senior Software Engineer
Top Expert 2009
Commented:
The postback itself does not use view state unless the control requires it in order to keep state.
Web forms is very messy and you are not the only one having to deal with this challenge. Suffice to say that in reality the postback is a form submit in disguise and the server event click is just a convenience  that ASP.NET offers to emulate winform event handling so that desktop developers can make a transition to web forms.

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial