Javascript – Button click/page submit from JavaScript

asp.netcjavascript

I'd like to understand how the asp.net framework knows when a button has been clicked and therefore once it receives the request fires its click event on the server.

I need to understand how it works as I would like to trigger the button's server click event from javascript.

I am able to perform a submit of the page from JavaScript:

 document.forms[0].submit();

But how could do the same so that asp.net thinks that a button click has happened, so on the server its click event fires once the request is received.

Thanks

UPDATE 1

Thanks everybody for the replies. Doing a few tests if I add one asp.net button and by looking at the source code, I cant seem to find a generated JavaScript "__doPostBack" function.

This is the test I've tried.

I have one normal HTML button (button1) and an asp.net button.(button2)

From the HTML button's click event (button1), I've added a client call to the javascript __doPostback method passing in the asp.net button2 id.

So I am trying to fire the server side button2 click event by calling the html button (button1.)

<html xmlns="http://www.w3.org/1999/xhtml">  
<head runat="server">  
    <title></title>  
</head>  
<body>  
    <form id="form1" runat="server">  
    <div>  
        <input id="Button1" type="button" 
         value="Raise Button2 server side click event"
         onclick="javascript:__doPostBack('Button2', '')" /> 

        <asp:Button ID="Button2" runat="server" 
        Text="asp.net_Button2" 
        OnClick="button2Click" />  
    </div>  
    </form>  
</body>  
</html> 

And the generated HTML:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head><title>
</title>
</head>
<body>
    <form name="form1" method="post" action="Default6.aspx" id="form1">
<div>
<input type="hidden" name="__VIEWSTATE" id="__VIEWSTATE" 
value="/wEPDwUKMjA0OTM4MTAwNGRke29WUkB+FLhQArxd1ehT98cWw1Y=" />
</div>

<div>

    <input type="hidden" name="__EVENTVALIDATION" id="__EVENTVALIDATION" 
value="/wEWAgKLs+3sDgK7q7GGCBs2MBvLk9CJNnA02t08R1LKdiJU" />
</div>
    <div>
        <input id="Button1" type="button" value="Raise Button2 server side click event" 
onclick="javascript:__doPostBack('Button2', '')" />
        <input type="submit" name="Button2" value="asp.net_Button2" id="Button2" />
    </div>
    </form>
</body>
</html>  

UPDATE 2

If I set the buttons "UseSubmitBehavior" attribute to false, then it does generated the __doPostBack method and by calling it from the client it does raise the event on the server.

So what would be the difference between submit and a postback? (I'll start a new thread for this).
Thanks for the help!

Best Answer

The javascript __doPostBack function is called whenever the page is needed to be posted back.

To learn about __doPostBack, read this article.


EDIT

Use the UseSubmitBehavior property to specify whether a Button control uses the client browser's submit mechanism or the ASP.NET postback mechanism. By default the value of this property is true, causing the Button control to use the browser's submit mechanism. If you specify false, the ASP.NET page framework adds client-side script to the page to post the form to the server.

When the UseSubmitBehavior property is false, control developers can use the GetPostBackEventReference method to return the client postback event for the Button. The string returned by the GetPostBackEventReference method contains the text of the client-side function call and can be inserted into a client-side event handler.


Refer to:

http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.button.usesubmitbehavior.aspx

http://www.w3schools.com/ASPNET/prop_webcontrol_button_usesubmitbehavior.asp