1

I have a page where a javascript will be triggered when the user closes the browser tabs / browser window. Language is in c# thanks.

im using window.onbeforeunload, something along the line like this:

<script type="text/javascript">


    window.onbeforeunload = myFunction;


function myFunction()
{
    //call my function here
}

the code behind function will be a simple function for now, so no input parameters or return value are needed. So i'll just like to know how to call my function (eg: public void callMyFunction())

5
  • 2
    I think you're confused here... javascript code runs in the browser (client) and C# code runs in the server. You can't call C# code from javascript. At most, you could call a webservice exposed on the server via an AJAX call. Commented Apr 29, 2010 at 10:19
  • 1
    Or use Page Methods. Good tutorial: singingeels.com/Articles/Using_Page_Methods_in_ASPNET_AJAX.aspx Commented Apr 29, 2010 at 10:35
  • @axel_c~ What makes you think that you can't call c# code from javascript? I've done it many times.. Just call button.click() from your javascript and be suprised! Commented Jan 5, 2011 at 7:27
  • @Pabuc that's triggering a client-side event that happens to post back to the server and execute C# code, not directly calling C# code from javascript. It's a different thing. Commented Jan 5, 2011 at 15:10
  • well.. Then you should have said "You can't write C# code into javascript function". Commented Jan 5, 2011 at 15:17

3 Answers 3

3

There are variety mechanisms to call your code-behind function(s). You could use an Ajax Call, Page Methods, ASP.NET Client Callbacks, or even trigger a code-behind handler using an invisible ASP.NET button.

Sign up to request clarification or add additional context in comments.

Comments

0

Try the sample code below:

<html>
<head>
    <script src="scripts/jquery-1.4.4.min.js" type="text/javascript"></script>

    <script type="text/javascript" language="javascript">
        windown.OnUnload(){ function(){ $("#Btn_Click").click();}};
    </script>

</head>
<body>
...

 <asp:Button ID="Btn_Click" runat="server" Text="ButtonClick" onClick="Btn_Click" />
...
</body>
</html>

hope it helps !!!

Comments

0

By using Ajax you can access code behind method from javascript . Try this code.

<script  type="text/javascript">
     classname.methodname();
    </script>

In code behind page: in page load you need to register follwing code...

pageload()
{
      AjaxPro.Utility.RegisterTypeForAjax(typeof(pagename), this.Page);
}



[AjaxPro.AjaxMethod(AjaxPro.HttpSessionStateRequirement.ReadWrite)]
    public void methodname()
    {
    ..........
    ........
    }

Here classname is code behind page class name

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.