简体   繁体   English

从aspx中的javascript调用函数背后的代码

[英]Call a code behind function from javascript in aspx

I have a page where a javascript will be triggered when the user closes the browser tabs / browser window. 我有一个页面,当用户关闭浏览器选项卡/浏览器窗口时,将触发JavaScript。 Language is in c# thanks. 语言是C#,谢谢。

im using window.onbeforeunload, something along the line like this: 我正在使用window.onbeforeunload,类似这样的东西:

<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()) 所以我只想知道如何调用我的函数(例如:public void callMyFunction())

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 . 您可以使用Ajax调用 ,页面方法, ASP.NET客户端回调 ,甚至可以使用不可见的ASP.NET按钮触发代码隐藏处理程序。

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 . 通过使用Ajax,您可以从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 这里的类名是页面类名后面的代码

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM