Posted by & filed under Web Development.

ASP.NET web forms page have a JavaScript method for handling PostBacks called “__doPostBack”.  This function is used to submit button clicks back to the server.

function __doPostBack(eventTarget, eventArgument) {
    if (!theForm.onsubmit || (theForm.onsubmit() != false)) {
        theForm.__EVENTTARGET.value = eventTarget;
        theForm.__EVENTARGUMENT.value = eventArgument;
        theForm.submit();
    }
}

The cool thing is that you can take advantage of the same methods that the framework uses to generate javascript for button clicks.  Use the following in your code file to generate the JavaScript that performs the PostBack. Using this method will ensure that the proper ClientID for the control is used.

protected string GetLoginPostBack()
{
    return Page.ClientScript.GetPostBackEventReference(btnLogin, string.Empty);
}

Then in the ASPX page add a javascript block.

<script language="javascript">
function btnLogin_Click() {
  <%= GetLoginPostBack() %>;
}
</script>

The final javascript will be rendered like this.

<script language="javascript">
function btnLogin_Click() {
  __doPostBack('btnLogin','');
}
</script>

Now you can use “btnLogin_Click()” from your javascript to submit the button click to the server.

6 Responses to “Calling a button click from JavaScript in ASP.NET”

  1. Johnf763

    If you are going for best contents like myself, just pay a quick visit this web page everyday since it offers feature contents, thanks cdakgacadgbe

  2. family guy the quest for stuff hack download

    The best part of this app, you can make a home entertainment, these games.

    Lots of family guy the quest for stuff hack large gamers in Europe, North America and Asia Pacific will remain low.
    I like this post we are in great shape. Now there is a good option to family guy the quest
    for stuff hack try it.

Leave a Reply

  • (will not be published)