简体   繁体   English

服务器端代码执行后,从asp.net代码中调用javascript函数

[英]Call javascript function from asp.net code behind after server side code executes

I have an asp.net button, that when clicked calls a code behind function. 我有一个asp.net按钮,单击该按钮会调用函数背后的代码。 The function does some evaluation, and then I want to call javascript from within this asp.net function. 该函数进行一些评估,然后我要从此asp.net函数中调用javascript。

While pranay's answer is correct in the function call, the rest is off, here's hopefully a better explanation: 虽然在函数调用中pranay的答案是正确的,但其余函数已关闭,希望这里有更好的解释:

You can use ClientScript.RegisterStartupScript() to do what you want, like this: 您可以使用ClientScript.RegisterStartupScript()来执行所需的操作,如下所示:

void myButton_Click(object sender, EventArgs e)
{
  var script = "alert('hi');";    
  ClientScript.RegisterStartupScript(typeof(Page), "ButtonAlert", script, true);
}

The format is (type, scriptKey, scriptText, wrapItInScriptTags) . 格式为(type, scriptKey, scriptText, wrapItInScriptTags)

If you are operating with an UpdatePanel, it's very similar, except you need to use the slightly different ScriptManager.ResgisterStartupScript() . 如果您使用UpdatePanel进行操作,则非常相似,只是需要使用略有不同的ScriptManager.ResgisterStartupScript() Use the UpdatePanel as the control and type parameters in that case to be safe. 在这种情况下,请使用UpdatePanel作为控件并输入参数,以确保安全。

JavaScript is a client side technology. JavaScript是一种客户端技术。

While I suppose it would be theoretically possible to run the code on the server I don't see what benefit it would give you. 虽然我认为理论上可以在服务器上运行代码是可能的,但我看不出它将为您带来什么好处。

What do you actually want to achieve? 您实际上想要实现什么?

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

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