Karthick
Karthick

Reputation: 11

How the server side identifies button click event

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.

Upvotes: 1

Views: 4317

Answers (3)

Adrian Iftode
Adrian Iftode

Reputation: 15663

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.

Upvotes: 13

walther
walther

Reputation: 13600

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.

Upvotes: 0

Anders Abel
Anders Abel

Reputation: 69260

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.

Upvotes: 2

Related Questions