[英]ASP.NET Call C# Functions From JavaScript
I Have a small working application on ASP.NET and C# and I would like to add some Javascript to it. 我在ASP.NET和C#上有一个小工作应用程序,我想添加一些Javascript到它。
I know that for example I can use the Confirm button and then do something on yes or no. 我知道例如我可以使用确认按钮,然后在是或否上做一些事情。
What I would like to do is call some of the functions I have in the code-behind, depending on the answer to the Confirm Button. 我想要做的是调用我在代码隐藏中的一些功能,具体取决于确认按钮的答案。
How do I go about this? 我该怎么做? Many Thanks!
非常感谢!
Simple. 简单。
Put a hidden div on your aspx page that contains button triggers to your methods: 在您的aspx页面上放置一个隐藏的div,其中包含按钮触发器的方法:
<div style="display: none">
<asp:Button ID="TriggerMethod1" runat="server" />
</div>
In your javascript, in the confirm part of your code simply do: 在您的javascript中,在代码的确认部分中,只需执行以下操作:
__doPostBack('TriggerMethod1', '');
And in your code-behind, handle up that button click to call Method1. 在您的代码隐藏中,处理该按钮单击以调用Method1。
I would create an ASHX handler file and post back to the hander using jQuery ajax
methods . 我将创建一个ASHX处理程序文件,并使用jQuery
ajax
方法回发给hander。
See an article on this here: 在这里看到一篇文章:
Using jQuery in ASP.NET apps with httphandlers 在ASP.NET应用程序中使用jQuery和httphandlers
To call a server side method on a client side event you need to do the following: 要在客户端事件上调用服务器端方法,您需要执行以下操作:
1- Create the server side method: 1-创建服务器端方法:
void DoSomething(...) { ... }
2- Implement the System.Web.UI.IPostBackEventHandler.RaisePostBackEvent
which take one string argument (You can assign the name to the value of this argument).: 2-实现
System.Web.UI.IPostBackEventHandler.RaisePostBackEvent
,它接受一个字符串参数(您可以将名称赋值给该参数的值):
public void RaisePostBackEvent(string eventArgument)
{
DoSomething(...);
}
3- Write a script to trigger post back: 3-编写脚本以触发回发:
function TriggerPostBack(control, arg){
__doPostBack(control, arg);
}
4- Call the PostBack trigger function when needed: 4-需要时调用PostBack触发器功能:
<a .... onclick="TriggerPostBack('control', 'arg')" .. />
Have you tried to use the search first in the site!! 您是否尝试过在网站中首先使用搜索!! Call ASP.NET function from JavaScript?
从JavaScript调用ASP.NET函数? Calling functions from an ASP.NET code file with javascript
使用javascript从ASP.NET代码文件调用函数
or https://stackoverflow.com/search?q=Call+C%23+Functions+From+JavaScript 或者https://stackoverflow.com/search?q=Call+C%23+Functions+From+JavaScript
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.