GeekInterview.com
Series: Subject: Topic:
Question: 8 of 348

How the server side identifies button click event

if i have placed the button control with the OnClick event like

Code
  1. <asp:Button ID="mybutton" runat="server" Text="Insert" onclick="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. <input type="submit" name="mybutton" value="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.
Asked by: karthime | Member Since Apr-2012 | Asked on: Apr 7th, 2012

View all questions by karthime

Showing Answers 1 - 2 of 2 Answers
Modather Sadik

Answered On : 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. <body>
  2.     <form method="post" action="Default.aspx" id="form1">
  3. <div class="aspNetHidden">
  4. <input type="hidden" name="__VIEWSTATE" id="__VIEWSTATE" value="/wEPDwUKLTU4NzY5NTcwN2Rk" />
  5. </div>
  6.  
  7. <div class="aspNetHidden">
  8.  
  9.         <input type="hidden" name="__EVENTVALIDATION" id="__EVENTVALIDATION" value="/wEWAwLyjNtAApm8wPAMAoLquO0O" />
  10. </div>
  11.     <div>
  12.         <input type="submit" name="btnRedirect" value="Redirect" id="btnRedirect" />
  13.         <input type="submit" name="btnEvent" value="text" id="btnEvent" />
  14.         <br />
  15.         <br />
  16.         <span id="lbl">Label</span>
  17.     </div>
  18.     </form>
  19. </body>

  
Login to rate this answer.
rupinder

Answered On : 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. <asp:Button />,  control renders by default an input of type="submit", which submits the form, using the browsers default mechanism.
  2. ASP .Net identifies which button was clicked, by checking the posted values.
  3. 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.
  4. So the name of one single submit input is sent to the server and in this way ASP .Net checks which button was clicked.
  5. ASP .Net generates some javascript which will do the job
  6. var theForm = document.forms[form1];    
  7. if (!theForm) {    
  8.     theForm = document.form1;    
  9. }
  10.  
  11. function __doPostBack(eventTarget, eventArgument) {    
  12.     if (!theForm.onsubmit || (theForm.onsubmit() != false)) {    
  13.         theForm.__EVENTTARGET.value = eventTarget;    
  14.         theForm.__EVENTARGUMENT.value = eventArgument;    
  15.         theForm.submit();    
  16.     }    
  17. }    
  18. //]]>    
  19. </script>
  20.  
  21. Notice _EVENTTARGET and _EVENTARGUMENT fields. These fields are set so ASP .Net will know which button was clicked on client.
  22. The value of EVENTTARGET will be read by ASP .Net and based on this will fire Click event of the <asp:Button /> control

  
Login to rate this answer.

Give your answer:

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

Related Open Questions

Ads

Connect

twitter fb Linkedin GPlus RSS

Ads

Interview Question

 Ask Interview Question?

 

Latest Questions

Interview & Career Tips

Get invaluable Interview and Career Tips delivered directly to your inbox. Get your news alert set up today, Once you confirm your Email subscription, you will be able to download Job Inteview Questions Ebook . Please contact me if you there is any issue with the download.