简体   繁体   English

如何在javascript中引发C#事件或函数

[英]how to raise a C# event or function in javascript

I have a function in my login page an it access is protect like other C# function by default. 我的登录页面中有一个功能,默认情况下它像其他C#功能一样受到保护。

protected void MyFunction()
{
    //do somthing
}

I want to raise it in JavaScript something like this 我想用JavaScript来提高它

<script>
    $.rise(MyFunction)
</script>

Someone said should use 有人说应该用

__doPostBack(control, arg);

Or 要么

<a .... onclick="TriggerPostBack('control', 'arg')" .. />

Or.... but all of this is for an ASP.NET object and if you use ASP.NET object it automatically generate callback and __doPostBack and everything you need. 或者....但是所有这些都是针对ASP.NET对象的,如果您使用ASP.NET对象,它将自动生成回调和__doPostBack以及您需要的所有内容。

Unfortunately I dont have an ASP.NET object and I use a simple HTML tag and want to raise ac# function by JavaScript manually. 不幸的是,我没有ASP.NET对象,我使用了一个简单的HTML标记,并希望通过JavaScript手动提高ac#函数。 is it possible? 可能吗? and the access of function what should be? 和功能的访问应该是什么? and about the security? 关于安全性? is it logical or illogical? 是合乎逻辑的还是不合逻辑的?

At the end I want to postback happen because I am familiar with jQuery AJAX and in this case I dont need it. 最后,我想回发,因为我熟悉jQuery AJAX,在这种情况下,我不需要它。 I want postback happen. 我希望发回邮件。

Change your method to a static webmethod and try an jquery ajax post. 将您的方法更改为静态web方法,然后尝试执行jquery ajax发布。

$.ajax({
    type: "POST",
    url: "YourPage.aspx/MyFunction",
    data: '',
    contentType: "application/json; charset=utf-8",
    dataType: "json",
    success: OnSuccess,
    failure: function(response) {
        alert(response.d);
    }
});

[WebMethod]
public static void MyFunction() 
{

}

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

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