How the server side identifies button click event

If i have placed the button control with the OnClick event like
Code
  1. span style="color: #ff0000;">"mybutton" runat="server" Text="Insert""mybutton_Click" />  
Copyright GeekInterview.com

and some other controls(buttons, textboxes) in .aspx page. When i run the page, the button displays in the page source of the browser like
Code
  1. span style="color: #ff0000;">"submit""mybutton""Insert" id="mybutton" />

  2.  
Copyright GeekInterview.com

here, it does not display the onclick event, then how the page calls the button click on the server side, how the server side identify which button cause the submit, and how this page moves to the server side.

Questions by karthime

Showing Answers 1 - 2 of 2 Answers

Modather Sadik

  • Oct 5th, 2012
 

The secret is in the

Tag and the hidden field "id="__EVENTVALIDATION", as you know that the form element supports the Event Attributes "means the abillity to fire scripts when an action happened like button click", The hidden field value maintains all possible postbacks of your controls throw javascript.

Code
  1. span style="color: #ff0000;">"post" action="Default.aspx" id="form1">

  2. <div class="aspNetHidden">

  3. <input type="hidden" name="__VIEWSTATE" id="__VIEWSTATE" value="/wEPDwUKLTU4NzY5NTcwN2Rk" />

  4. </div>

  5.  

  6. <div class="aspNetHidden">

  7.  

  8.         <input type="hidden" name="__EVENTVALIDATION" id="__EVENTVALIDATION" value="/wEWAwLyjNtAApm8wPAMAoLquO0O" />

  9. </div>

  10.     <div>

  11.         <input type="submit" name="btnRedirect" value="Redirect" id="btnRedirect" />

  12.         <input type="submit" name="btnEvent" value="text" id="btnEvent" />

  13.         <br />

  14.         <br />

  15.         <span id="lbl"

  Was this answer useful?  Yes

rupinder

  • Apr 17th, 2013
 

, control renders by default an input of type="submit", which submits the form, using the browsers default mechanism.
ASP .Net identifies which button was clicked, by checking the posted values.
When a browser submits a form it writes in the POST request the name and value of the clicked button among with the names and values of the other inputs, excluding the other submit inputs.
So the name of one single submit input is sent to the server and in this way ASP .Net checks which button was clicked.
ASP .Net generates some javascript which will do the job
var theForm = document.forms[form1];
if (!theForm) {
theForm = document.form1;
}

function __doPostBack(eventTarget, eventArgument) {
if (!theForm.onsubmit || (theForm.onsubmit() != false)) {
theForm.__EVENTTARGET.value = eventTarget;
theForm.__EVENTARGUMENT.value = eventArgument;
theForm.submit();
}
}
//]]>


Notice _EVENTTARGET and _EVENTARGUMENT fields. These fields are set so ASP .Net will know which button was clicked on client.
The value of EVENTTARGET will be read by ASP .Net and based on this will fire Click event of the control

Code
  1. span style="color: #ff0000;">"submit"//]]>    

  2. </script>

  3.  

  4. Notice _EVENTTARGET and _EVENTARGUMENT fields. These fields are set so ASP .Net will know which button was clicked on client.

  5. The value of EVENTTARGET will be read by ASP .Net and based on this will fire Click event of the <asp:Button /> control

  Was this answer useful?  Yes

Give your answer:

If you think the above answer is not correct, Please select a reason and add your answer below.

 

Related Answered Questions

 

Related Open Questions