我有一个按钮点击事件的代码:
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>
这不会显示消息。也许我的 RegisterStartupScript 不正确?
最佳答案
尝试以下,
您必须为您的脚本提供一个有效的名称。 http://msdn.microsoft.com/en-us/library/z9h4dk8y.aspx
A startup script is uniquely identified by its key and its type. Scripts with the same key and type are considered duplicates. Only one script with a given type and key pair can be registered with the page. Attempting to register a script that is already registered does not create a duplicate of the script.
ShowMessage
应该在页面中定义您必须将参数作为字符串传递。
ShowMessage('"+ message +"');
最终代码应该是这样的
Page.ClientScript.RegisterStartupScript(GetType(), "myAlertScript", "ShowMessage('"+ message +"');", true);
更新
如果页面中有更新面板,请尝试使用以下代码。
ScriptManager.RegisterClientScriptBlock(this,typeof(Page),"myAlertScript","ShowMessage('" + message +"');", true);
关于javascript - 在服务器 Onclick 事件之后调用 Javascript 函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16787302/