Pages

_doPostBack function in javascript - asp.net postback mechanism

Big question!!! What is __doPostBack function in javascript and what it does? In asp.net all the web server controls except Button and ImageButton (discussed at the end) use javascript __doPostBack function to trigger postback. This behavior is known as asp.net postback mechanism.

To better understand this function lets go step by step. Lets take any web server control except Button and ImageButton on a sample aspx page.
///ASPX
<asp:LinkButton ID="LinkButton1" runat="server" OnClick="LinkButton1_Click" Text="LinkButton" />

///Code behind
protected void LinkButton1_Click(object sender, EventArgs e) { }

Run the page in browser and view it source.
///JavaScript
function __doPostBack(eventTarget, eventArgument) {
    if (!theForm.onsubmit || (theForm.onsubmit() != false)) {
        theForm.__EVENTTARGET.value = eventTarget;
        theForm.__EVENTARGUMENT.value = eventArgument;
        theForm.submit();
    }
}

///HTML
<input type="hidden" name="__EVENTTARGET" id="__EVENTTARGET" value="" />
<input type="hidden" name="__EVENTARGUMENT" id="__EVENTARGUMENT" value="" />

<a id="LinkButton1" href="javascript:__doPostBack('LinkButton1','')">LinkButton</a>

As you can see _doPostBack function takes two arguments viz. eventTarget and eventArgument. Moreover two hidden fields, “__EVENTTARGET” and “__EVENTARGUMENT,” are automatically declared which are used to store values of 'eventTarget' and 'eventArgument' respectively.

When LinkButton1 is clicked, __doPostBack function in javascript is called passing it respective arguments, in this case eventTarget is LinkButton1 and eventArgument is empty, these values are stored in respective hidden fields i.e. they are added to form parameter collection and finally the form is submitted. On server, ASP.NET retrieves and uses the values from form collection and understands which control caused the postback.

Ok if you have some confusion on these values are retrieved, lets do it ourselves to clear the things out. Take another control on web form, say another link button 'LinkButton2'.


Insert break point on Page_Load event and hit F5. Click on LinkButton1, check the values that are retrieved in Page_Load event.


As you see __EVENTTARGET key returns the value of the control that caused postback, here LinkButton1. Now click on LinkButton2 and check the value you get, it will be "LinkButton2". This is how the asp.net postback mechanism works through use of javascript __doPostBack function.

Button and ImageButton

These are the only two controls which cause postback in asp.net. Rest (as explained above) use javascript __doPostBack function and post the form (ASP.NET postback mechanism). In other words these two controls use client browser's submit behavior and hence they do not use __doPostBack function.

However you can make Button to use asp.net postback mechanism by making use of Button.UseSubmitBehavior property (this property is not available for ImageButton control). UseSubmitBehavior property is of type bool and decides whether Button should use client browser's submit behavior (Default. value = true) or asp.net postback mechanism (value = false).

Lets take two Button control on page:
<asp:Button ID="Button1" runat="server" Text="Uses browser submit behavior" UseSubmitBehavior="true" />
<asp:Button ID="Button2" runat="server" Text="Uses asp.net postback mechanism" UseSubmitBehavior="false" />
Note that you don't have to specify UseSubmitBehavior="true" explicitly as it is the default value/behavior. I have added it just for the sake of understanding. Now run this page and view its source in browser, you will these buttons rendered as follows:
<input type="submit" name="Button1" value="Uses browser submit behavior" id="Button1" />
<input type="button" name="Button2" value="Uses asp.net postback mechanism" onclick="javascript:__doPostBack('Button2','')" id="Button2" />
Button2 for which we set UseSubmitBehavior property to false, will now use _doPostBack function to submit the form.

Cheers!

17 comments:

  1. good blog post, very informative

    ReplyDelete
  2. Hi,

    This is a very informative article. Thanks for sharing your knowledge. I have findout few other links that also descrebed in a good way about (PostBack,_doPostBack

    and AutoPostBack).

    http://mindstick.com/Articles/50855cbe-657e-41fa-a7b2-7835cfa1dba2/?PostBack%20in%20ASP%20Net

    http://www.c-sharpcorner.com/uploadfile/2f73dd/what-is-postback-in-Asp-Net/

    http://www.codeproject.com/Articles/68371/Detecting-Refresh-or-Postback-in-ASP-NET

    ReplyDelete
  3. Greetings! I've been reading your blog for a while now and finally
    got the bravery to go ahead and give you a shout out from Houston Tx!
    Just wanted to say keep up the good work!

    Also visit my website ... garden gnomes

    ReplyDelete
  4. Useful stuff thanks :) I have written an post about another way of getting an ASP Button to postback which might be useful: http://techygypo.blogspot.com/2013/09/how-to-use-dopostback-with-aspnet-button.html

    ReplyDelete
  5. Caraw testei aqui num funcionou =(, tenta colocar o codigo inteiro

    ReplyDelete
    Replies
    1. Friend tested here, but it does not work =(, tries to put the whole code.

      Delete
  6. do like this
    __doPostBack('<%= savebtn.UniqueID %>', "

    ReplyDelete
  7. Hi there everybody, here every person is sharing these kinds of knowledge, tthus it's pleasant to read this web site,
    and I used to go to see his weblog everyday.

    My homepage numerology reading born 16th june 1949

    ReplyDelete
  8. I've followed the example here and achieved the form postback, but it was my belief that click event handler on the triggering button would be invoked. However, that doesn't seem to be the case. Can someone please comment on this?

    ReplyDelete
  9. this code failed after deploying to server worked fine in localhost

    ReplyDelete
  10. It works only one problem it is triggering postback event twice.
    Can anyone share better solution ?

    ReplyDelete
  11. After visiting lots of websites finally i got solution. This is a very useful article. Thanks for sharing your knowledge.

    ReplyDelete