hi i have one doubt of button click in asp.net, i have placed the button control with the OnClick event like 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 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.
-
1http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.button.usesubmitbehavior.aspx ... http://stackoverflow.com/a/4635993/284240 – Tim Schmelter Apr 07 '12 at 12:12
3 Answers
If it is about <asp:Button />
, this control renders by default an input of type="submit", which submits the form, using the browser's 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.
This server control has also the property UseSubmitBehavior and if this is set to false it will generate an input of type button. Since buttons don't submit the forms, ASP .Net generates some javascript which will do the job
<script type="text/javascript">
//<![CDATA[
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();
}
}
//]]>
</script>
Notice _EVENTTARGET and _EVENTARGUMENT fields. These fields are set so ASP .Net will know which button was clicked on client.
<input type="button"
name="rptMain$ctl02$DeleteButton"
value="Delete" onclick="__doPostBack('rptMain$ctl02$DeleteButton','')"
id="rptMain_ctl02_DeleteButton" />
Notice the arguments passed to the __doPostBack function. The name of the input is passed and set to EVENTTARGET. The value of EVENTTARGET will be read by ASP .Net and based on this will fire Click event of the <asp:Button />
control
EVENTARGUMENT is used by ItemCommand events, usually placed in the DataBound controls and contains specific data about that action, going to be used later when handling the event. This data can be for example an ID of row in a database table.

- 15,465
- 4
- 48
- 73
-
2This kind of answers is what keeps me coming back to stack overflow. Detailed, elaborated and informative. I've learnt some new stuff. +1. – Anders Abel Apr 07 '12 at 16:49
-
Hi thank you for your reply, if the usesubmitbehaviour is set to true how the event of the button control will be invoked – Karthick Apr 09 '12 at 17:52
-
It will generate an input of type submit (this is the default behavior) – Adrian Iftode Apr 10 '12 at 07:44
In ASP.NET web forms all buttons cause a submit of the entire form to the server. The Web forms engine checks what button was clicked and calls the appropriate event handlers.

- 67,989
- 17
- 150
- 217
If you specify onclick attribute of a control, it doesn't get passed to the client like this. Instead, it only specifies the wiring for the IIS when it parses the aspx file
Alternative looks something like this:
myControl.Click += new EventHandler(specifiedMethod);
So when the event occurs, it knows which method to call.

- 13,466
- 5
- 41
- 67