Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How does ASP.NET know which Button was clicked in order to raise its event

When using a LinkButton the doPostBack() function is rendered in the page and when the button is clicked doPostBack() is invoked at client side submitting the form to the server and also sending information in two hidden fields called EVENTTARGET and EVENTARGUMENT. The EVENTTARGET holds the control name which caused the postback and EVENTARGUMENT holds any info.

With this ASP.NET engine gets the control name from EVENTTARGET argument which caused postback and calls RaisePostBackEvent() event to call right server side event handler. I understand how doPostBack() works and how server side event handler is called.

However for a normal server button how is the correct button click at server side called? How is ASP.NET notified on the control name that was clicked client side?

like image 785
Thomas Avatar asked Oct 07 '22 19:10

Thomas


1 Answers

A normal ASP.NET Button is rendered as a HTML <input type="submit"> element with a unique name attribute. By default this HTML element will trigger an HTTP POST to the HTML form associated with the current ASPX page.

When an HTTP POST happens a sequence of control-name/current-value pairs, known as form data set, is constructed from the successful controls inside the form, for example the textual value inside a text box is transmitted like this. A pair with the name of the button that triggered the submit is also sent to the server and that's how ASP.NET know which button triggered the HTTP POST.

The HTML specification guarantees that only the successful controls inside the form are considered for the form data set that is posted and since the definition of a successful control contains the following rule:

If a form contains more than one submit button, only the activated submit button is successful.

then ASP.NET can check the posted form data set to check if any normal button was triggered.

like image 85
João Angelo Avatar answered Oct 11 '22 00:10

João Angelo