1

I have this code for a click event of a button:

protected void Button1_Click(object sender, EventArgs e)
{
    string message = "";
    //Series of codes to change the message variable

    Page.ClientScript.RegisterStartupScript(GetType(), "", "ShowMessage(" + message +");", true);
    }



<script type="text/javascript">
    function ShowMessage(msg){
        alert(msg);
    }
</script>

and this won't display the message. maybe my RegisterStartupScript is incorrect?

4

4 回答 4

3

尝试以下,

  1. 您必须为您的脚本提供一个有效的名称。http://msdn.microsoft.com/en-us/library/z9h4dk8y.aspx

    启动脚本由其键和类型唯一标识。具有相同键和类型的脚本被认为是重复的。页面中只能注册一个具有给定类型和密钥对的脚本。尝试注册已注册的脚本不会创建该脚本的副本。

  2. ShowMessage应该在页面中定义

  3. 您必须将参数作为字符串传递。ShowMessage('" + message +"');

  4. 最终代码应该是这样的

Page.ClientScript.RegisterStartupScript(GetType(), "myAlertScript", "ShowMessage('" + message +"');", true);

更新


如果页面中有更新面板,请尝试以下代码。

ScriptManager.RegisterClientScriptBlock(this,typeof(Page),"myAlertScript","ShowMessage('" + message +"');", true);
于 2013-05-28T08:35:30.103 回答
2

message在调用 时缺少变量周围的引号,请ShowMessage尝试以下操作:

Page.ClientScript.RegisterStartupScript(this.GetType(), "", "ShowMessage(\"" + message + "\");", true);
于 2013-05-28T08:34:58.427 回答
0

要测试消息是否已传递到客户端,您只需调用本机 javascript 函数即可排除任何客户端问题。

Page.ClientScript.RegisterStartupScript(GetType(), "", "alert('here?');", true);

您还可以在浏览器中查看页面的源代码,以查看代码是否正在交付给客户端。

于 2013-05-28T08:35:11.677 回答
0

要在单击处理程序(及其调用的任何内容)完成执行代码,请在处理程序期间设置超时

function onClick() {
    window.setTimeout(functionToCall, 0);
}
于 2013-05-28T08:38:42.463 回答