本文介绍了从呼叫asp.net code javascript函数服务器端code执行后被背后的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个asp.net按钮,单击时调用函数后面一个code。该功能做了一些评估,然后我想从这个asp.net函数中调用JavaScript。

I have an asp.net button, that when clicked calls a code behind function. The function does some evaluation, and then I want to call javascript from within this asp.net function.

推荐答案

虽然pranay的答案是在函数调用正确的,其余的是关闭的,这里是希望能更好的解释:

While pranay's answer is correct in the function call, the rest is off, here's hopefully a better explanation:

您可以使用<$c$c>ClientScript.RegisterStartupScript()做你想做的,是这样的:

You can use ClientScript.RegisterStartupScript() to do what you want, like this:

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

如果您正在使用一个UpdatePanel工作,这是非常相似的,除了你需要使用的的不同的<$c$c>ScriptManager.ResgisterStartupScript().使用在UpdatePanel如在这种情况下,控制和类型参数是安全的。

If you are operating with an UpdatePanel, it's very similar, except you need to use the slightly different ScriptManager.ResgisterStartupScript(). Use the UpdatePanel as the control and type parameters in that case to be safe.

这篇关于从呼叫asp.net code javascript函数服务器端code执行后被背后的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-27 18:52