【问题标题】:Can't call alert from Server Side无法从服务器端调用警报
【发布时间】:2014-11-28 15:31:55
【问题描述】:

在 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);
                            }

【问题讨论】:

    标签: javascript c# asp.net webforms clientscript


    【解决方案1】:

    试试这个, 在 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);
        }
    

    然后在表格中

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

    【讨论】:

      【解决方案2】:

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

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

      【讨论】:

      • 如果像这样script.RegisterClientScriptBlock(this.GetType(), "text", "SignOffAlert()"); 也不想工作
      • em,等一下,在我呈现警报之前我得到了异常
      • 我更新了答案
      猜你喜欢
      • 1970-01-01
      • 2022-08-17
      • 1970-01-01
      • 1970-01-01
      • 2011-08-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-10-21
      相关资源
      最近更新 更多