C# 在后面的代码中单击按钮执行 JavaScript
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11246948/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Executing JavaScript on a Button Click in Code Behind
提问by
Possible Duplicate:
How to call javascript function from c#
可能的重复:
如何从 C# 调用 javascript 函数
Is there a way to call a javascript function from a button click in code behind in C#? So basically I have a registration form and when you click the button, then all the data is processed in the code behind and the form is submitted. But I need to execute some javascript and I need it to be executed on the button click and in code behind.
有没有办法从 C# 后面的代码中单击按钮调用 javascript 函数?所以基本上我有一个注册表单,当您单击按钮时,所有数据都在后面的代码中处理并提交表单。但是我需要执行一些 javascript 并且我需要在按钮单击和后面的代码中执行它。
so something like:
所以像:
protected void btnDoRegister_Click(object sender, System.Web.UI.ImageClickEventArgs e)
{
C# code is here doing form processing
// Now I need to Execute javascript here.
}
Is this possible and how would I go about doing it, if it is?
这是可能的,如果可能,我将如何去做?
Thanks!
谢谢!
采纳答案by codingbiz
protected void btnDoRegister_Click(object sender, System.Web.UI.ImageClickEventArgs e)
{
C# code is here doing form processing
// Now I need to Execute javascript here.
string script = "<script type=\"text/javascript\"> functionToExecute(); </script>";
ClientScript.RegisterClientScriptBlock(this.GetType(), "myscript", script);
}
回答by corsiKa
There's two ways I see off the top of my head:
我有两种方式可以看出我的头顶:
The first is do some AJAX polling on the javascript side. It can poll for changes it needs to make via a rest api or something, and your c# code can update that list of changes.
第一个是在 javascript 端做一些 AJAX 轮询。它可以通过 rest api 或其他方式轮询需要进行的更改,并且您的 c# 代码可以更新该更改列表。
The second is to use a javascript socket io implementation and maintain a persistent connection.
第二种是使用javascript socket io实现并保持持久连接。
I'd highly recommend the first, but the second is an option depending on your environment.
我强烈推荐第一个,但第二个是取决于您的环境的选项。
回答by Adil
Executing javascript on server is not possible as javascript executes on client(browser). You can execute javascript after postback but it is seldom required. Normally Javascript executes on client side and executes before the request is received at server and executed on server. This way you have two event binded with btnDoRegister one executes on client and ohter executes on server.
由于 javascript 在客户端(浏览器)上执行,因此无法在服务器上执行 javascript。您可以在回发后执行 javascript,但很少需要。通常 Javascript 在客户端执行并在服务器收到请求并在服务器上执行之前执行。这样你就有两个与 btnDoRegister 绑定的事件,一个在客户端执行,另一个在服务器上执行。
On Page_Load bind javascript event with button.
在 Page_Load 上用按钮绑定 javascript 事件。
In Code Behind
背后的代码
btnDoRegister.Attributes.Add("onclick", "funForButton();");
In aspx HTML
在 aspx 中
<script language="javascript" type="text/javascript">
function funForButton()
{
alert("About to send request to server");
}
</script>
The Server event you already have
您已经拥有的服务器事件
protected void btnDoRegister_Click(object sender, System.Web.UI.ImageClickEventArgs e)
{
C# code is here doing form processing
// Now I need to Execute javascript here.
}
回答by ZorbaSan
If i understand correctly, you want to click the button, have some javascript do its thing, then return to the serverside, do the C# code and finish it off with executing some javascript (on the client again).
如果我理解正确,您想单击该按钮,让一些 javascript 做它的事情,然后返回到服务器端,执行 C# 代码并通过执行一些 javascript(再次在客户端上)完成它。
Let's assume i got it right then you would have to do 3 things.
让我们假设我做对了,那么你必须做 3 件事。
First, add the OnClientClick="return yourfunction();" to the button in your aspx page. Depending on if you want the serverside code to be executed, make sure the yourfunction() in javascript returns false or true.
首先,添加 OnClientClick="return yourfunction();" 到您的 aspx 页面中的按钮。根据您是否希望执行服务器端代码,请确保 javascript 中的 yourfunction() 返回 false 或 true。
Then code your c# code in the btnDoRegister_Click. And at the end of this, add a javascript call to a session variable and use this in your aspx code.
然后在 btnDoRegister_Click 中编写您的 c# 代码。最后,向会话变量添加一个 javascript 调用,并在您的 aspx 代码中使用它。
example: c#
示例:c#
String newScript = ""
If (Session("yourscript") = Nothing) { Session("yourscript") = "" }
newScript = String.Format("<script type=text/javascript>")
newScript = String.Format("{0}yoursecondscript()", newScript)
newScript = String.Format("{0}</script>", newScript)
Session("yourscript") = newScript
and then in your aspx page add somewhere in your page:
然后在您的 aspx 页面中添加页面中的某处:
<%=Session("yourscript")%>
Keep in mind where to place it if you are using Ajax because not the entire page will be refreshed. In that case, place it inside the updateframe.
如果您使用 Ajax,请记住将它放在哪里,因为不会刷新整个页面。在这种情况下,请将其放在 updateframe 内。
Now the sequence is: - click -> yourfunction() -> C# code -> yoursecondscript
现在的顺序是: - 点击 -> yourfunction() -> C# code -> yoursecondscript
hope this helps....
希望这可以帮助....

