To execute Javascript from the Button click and then run the server side code.

 An easy ways to do this is to call the __doPostBack() function of the javascript. An example is as follows:

 <input type="button" id="Button2" value="Button 2 Click" onclick="DoPostBack()" />


<script language="javascript" type="text/javascript">

function DoPostBack()
{
  //Any javascript statements....

__doPostBack('Button2','My Argument');
}

</script>
 

However in  Visual Studio 2005, the server and the client code can be directly run from the button attributes only.

OnClick property: For server side scripts

OnClientClick: For client side scripts

E.g. 

<asp:Button ID="btnClickMe" runat="server" Text="Click Me" OnClick ="btnClickMe_Click"  OnClientClick="DoPostBack();" />


 

5 Comments

  • I am trying this right now to call both server and client side functions, but I cannot control the order they are called. I have a ReportViewer that needs to be refreshed in the code behind, and then have the clientside call ReportViewer ClientController.LoadPrintControl()

    Any thoughts on how to solve this problem?

    I am trying to Render a report and bring up the print dialogue with a single click and not have the user see the report at all.

  • dear srusaggs, the way I would do it is register a javascript with the code you need on the page in the codebehind that handles the server event.

  • i am using reportviewer control vs2008, i am trying print using
    document.getElementById( 'rptviewer1).ClientController.LoadPrintControl(); this is returing false.
    here LoadprintControl is searching "printframerptviewer1" since id is rptviewer1.

  • help me for my previous comment.

  • Great Script just what I needed :)

    Thank you

Comments have been disabled for this content.