最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

c# - Call a code behind function from javascript in aspx - Stack Overflow

programmeradmin0浏览0评论

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())

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())

Share Improve this question asked Apr 29, 2010 at 10:15 newNamenewName 855 silver badges8 bronze badges 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. – axel_c Commented Apr 29, 2010 at 10:19
  • 1 Or use Page Methods. Good tutorial: singingeels./Articles/Using_Page_Methods_in_ASPNET_AJAX.aspx – Joop 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! – Pabuc 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. – axel_c Commented Jan 5, 2011 at 15:10
  • well.. Then you should have said "You can't write C# code into javascript function". – Pabuc Commented Jan 5, 2011 at 15:17
Add a ment  | 

3 Answers 3

Reset to default 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.

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 !!!

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

发布评论

评论列表(0)

  1. 暂无评论