无法从服务器端调用警报

时间:2014-11-28 15:31:55

标签: javascript c# asp.net webforms clientscript

在Web窗体项目中,我需要打开警报。

我尝试这样做

var script = Page.ClientScript;
                            if (!script.IsClientScriptBlockRegistered(this.GetType(), "SignOffAlert"))
                            {
                                script.RegisterClientScriptBlock(this.GetType(), "text", "SignOffAlert");
                            }

并在视图

上添加js-function
function SignOffAlert() {
            alert('The form cannot be submitted!');
        }

按钮点击警告不存在


更新:

此代码适用于我

var script = Page.ClientScript;
                            if (!script.IsClientScriptBlockRegistered(this.GetType(), "signOffAlert"))
                            {
                                script.RegisterClientScriptBlock(this.GetType(), "signOffAlert", "alert('The form cannot be submitted!');", true);
                            }

2 个答案:

答案 0 :(得分:0)

你需要注册完整的脚本,我相信

你在这看过这个例子吗? http://msdn.microsoft.com/it-it/library/bahh2fef%28v=vs.110%29.aspx

答案 1 :(得分:0)

试试这个, 在Page_Load写这个:

string myscript = "  <script> function SignOffAlert() { alert('The form cannot be submitted!');}</script>";

    if (! Page.ClientScript.IsClientScriptBlockRegistered(this.GetType(), "SignOffAlert"))
    {
        Page.ClientScript.RegisterClientScriptBlock(this.GetType(), "text", myscript);
    }

然后在Form

<asp:Button ID="Button1" runat="server" Text="Button" OnClientClick="SignOffAlert()" />