9

I used an <asp:Button /> control, and after rendering in the browser that control doesn't have a click event property assigned. How exactly is it calling the sever side event?

ASPX code:

<asp:Button ID="Button1" runat="server" Text="Button" OnClick="TestClickEvent" />

The above control was rendered in browser as following code:

<input type="submit" name="Button1" value="Button" id="Button1">

The following code is rendered in the browser, which sets __EVENTTARGET. My doubt is how does the __doPostBack method get called? Where is the calling method?

 function __doPostBack(eventTarget, eventArgument) {
        if (!theForm.onsubmit || (theForm.onsubmit() != false)) {
            theForm.__EVENTTARGET.value = eventTarget;
            theForm.__EVENTARGUMENT.value = eventArgument;
            theForm.submit();
        }
    }
Prabhakaran Parthipan
  • 4,193
  • 2
  • 18
  • 27
  • 3
    Show us your code please – Michiel van Vaardegem Mar 28 '13 at 09:42
  • _"after rendering in browser"_ I think you're confusing server- and clientside code. – Tim Schmelter Mar 28 '13 at 09:44
  • I think you should read this http://support.microsoft.com/kb/306459 it explains Web Forms serverside controls. In essence postbacks are done via javascript, the __EVENTTARGET and __VIEWSTATE hidden fields hold information about what was clicked and the data on the page respectively. Behine the scenes __EVENTTARGET etc are interrogated and the appropriate server-side event handler is called. – Luke Baughan Mar 28 '13 at 09:46
  • As you can see, your button is a 'submit'. Your asp.net form tag in your rendered HTML source catches this submit, and knows who the sender is, so it can fire the correct event – Michiel van Vaardegem Mar 29 '13 at 07:55
  • Please read this. http://aspalliance.com/895_Understanding_the_JavaScript___doPostBack_Function.all and there are plenty of question exists in SO already. http://stackoverflow.com/questions/3591634/how-to-use-dopostback, Basicially every thing works under the HTTP Pipeline. – Ravi Gadag Apr 01 '13 at 15:50
  • and this http://www.evagoras.com/2011/02/10/how-postback-works-in-asp-net/ . a simple google search would have yielded you a number of results. any way read those links :) – Ravi Gadag Apr 01 '13 at 15:56
  • @P.PrabhakaranB.E Are you looking for something more than my answer specifies? – Jesse Apr 08 '13 at 21:45
  • @Jesse ...your answer is good ..but After submitting button , how _dopostback() method calling and how the parameter passes.. – Prabhakaran Parthipan Apr 09 '13 at 04:55
  • @P.PrabhakaranB.E - That's the thing, the submit button **does not** use `__doPostBack`; there's no need for it. `__doPostBack` is only used by controls **other than** submit buttons. – Jesse Apr 09 '13 at 04:59
  • If _doPostBack method not fired means ..how __EVENTTARGET and __EVENTARGUMENT get initialized – Prabhakaran Parthipan Apr 09 '13 at 05:01
  • 1
    During a postback by the submit button, it is not required. When the ASP.NET Framework receives a POST with an empty `__EVENTTARGET` and `__EVENTARGUMENT`, it **automatically** knows that the submit button caused the postback. – Jesse Apr 09 '13 at 05:04
  • Suppose page has more than one submit button ,at that how its calling exactly that particular method.. – Prabhakaran Parthipan Apr 09 '13 at 05:07
  • let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/27814/discussion-between-jesse-and-p-prabhakaran-b-e) – Jesse Apr 09 '13 at 05:08

3 Answers3

13

The simple answer: The __doPostBack JavaScript function is called based on specific <asp /> controls and the events that it handles.

The detailed answer: It depends.


First, let's cover your example. You have an <asp:Button /> which is rendered as a standard <input type="submit" />. Everything in ASP.NET WebForms revolves around the standard HTML <form> tag. An HTML <form> is submitted without the use or assistance of JavaScript through clicking an <input type="submit" /> button.

With this in mind, you can very well see (which you've already noticed) that the rendered <input type="submit" /> button does not have an onclick event assigned. And, as you can see, the form is submitted when the button is clicked.

When it comes to how the back end (C#/VB.NET/etc.) code is executed when the <input type="submit" /> button is clicked: it is all handled by the ASP.NET Framework itself, and is beyond the scope of this question/answer.


Second, now let's cover what __doPostBack is, and how it is used. __doPostBack is simply a helper JavaScript function used to submit the HTML <form>. Due to the reasons outlined above, you now know why the <input type="submit" /> button does not need to call the __doPostBack function.

For simplicity, let's take a look at an ASP.NET page which has an <asp:DropDownList /> control, and it has the SelectedIndexChanged event handler assigned:

<asp:DropDownList ID="MyDropDownList" AutoPostBack="true" OnSelectedIndexChanged="MyDropDownList_SelectedIndexChanged" runat="server" />

The <asp:DropDownList /> is rendered as follows:

<select id="ctl00_MyDropDownList" onchange="javascript:setTimeout('__doPostBack(\'ctl00$MyDropDownList\',\'\')', 0)" name="ctl00$MyDropDownList"></select>

let's ignore the setTimeout function in the onchange event - it's merely a hacky workaround used by ASP.NET - and let's focus on the __doPostBack function inside of it.

As you can see here, the __doPostBack function is being called by the onchange event handler. The key difference is that changing the value of an <asp:DropDownList /> or <select /> control does not cause the browser to submit the form!

Once again the ASP.NET Framework handles internally how the back end code is executed when the form is submitted (whether through the __doPostBack function or not).


Lastly, as for the details of __doPostBack: it accepts two parameters - eventTarget and eventArgument. eventTarget contains the rendered HTML id property of the control which is causing the postback; and eventArgument is an optional parameter which can be used to pass additional data to the back end code.


Edit Additional Info: the OP asked a very interesting question - what happens when there is more than one submit button?

Well, during a POST operation, browsers include the value of the <input type="submit" /> which caused the operation to initiate.

This means, that just as you obtain the values of your <input /> elements, you can also query for which button caused the submit!

Jesse
  • 8,605
  • 7
  • 47
  • 57
  • wow really liked this explaination , have been using ASP.NET from last 5 years but didn't try to know how it actually work in this details !!! – Ali Apr 04 '13 at 18:45
4

Your control is a submit button, it can directly submit the form to the server without calling the __dopostback method. So in this case __dopostback is not getting called. __dopostback is used for controls not having a default submit behaviour, like dropdownlist.

The wireing to your server side event happens through the ipostbackeventhandler interface that your control implements. For more info please check out this http://msdn.microsoft.com/en-us/library/system.web.ui.ipostbackeventhandler.raisepostbackevent.aspx

Atti
  • 160
  • 10
  • without calling _dopostback method ,how __EVENTTARGET value get initialized. – Prabhakaran Parthipan Apr 01 '13 at 05:21
  • It wont get initialized and you don't need it, becouse the button submits the form directly, not through _dopostback, the asp.net framework knows who fired the event and it will call the ipostbackeventhandler interface RaisePostBackEvent method, which will wire it to the click event of the button – Atti Apr 04 '13 at 20:34
4

Well, the reason that you are having doubt is because you think function __doPostBack is needed to post. But it is other way round, when html was first designed, only way to post back to server was via submit button. This functionality was build in the browser them self, such that when ever the submit button was pressed browser passed the data of the form to the location declared. but as the web developed people started demanding for more. Now other controls also needed to post back to server. To implement them various hacks was developed, the most well known __doPostBack function is one of them. any control other that submit now can post back using __doPostBack function. But submit button do that themself. Thats all.

Ratna
  • 2,289
  • 3
  • 26
  • 50