【问题标题】:Message Box in ASP.NETASP.NET 中的消息框
【发布时间】:2009-12-07 05:45:59
【问题描述】:

如何在内容页面中显示消息框..? 更新个人资料后..我想在内容页面中显示一个消息框..

请提出您的建议.. 提前致谢。

【问题讨论】:

    标签: asp.net


    【解决方案1】:

    您可以使用Page.RegisterStartupScript 方法。

    if (UpdateProfile())
        Page.RegisterStartupScript("startup", "<script>alert('your profile has been updated..');</script>");
    

    当然假设 UpdateProfile() 完成工作并返回一个表示成功的布尔值:)

    或者(因为该方法已过时),您可以改用ClientScriptManager.RegisterStartupScript 方法。

    if (UpdateProfile())
        Page.ClientScript.RegisterStartupScript(this.GetType(), "startup", "<script>alert('your profile has been updated..');</script>", false);
    

    【讨论】:

    • 是的,您可以通过提供函数名称或脚本来执行任何 javascript,如果您没有任何具体原因,为什么不在页面本身而不是“alert()”上显示消息。
    • 这也有效,我想要最简单的东西,这在母版页中更正确。不知道脚本标签发生了什么,但是当我预览时它们被吃掉了。
    【解决方案2】:

    先写这个方法

    public void MsgBox(String ex, Page pg,Object obj)
        {
            string s = "<SCRIPT language='javascript'>alert('" + ex.Replace("\r\n", "\\n").Replace("'", "") + "'); </SCRIPT>";
            Type cstype = obj.GetType();
            ClientScriptManager cs = pg.ClientScript;
            cs.RegisterClientScriptBlock(cstype, s, s.ToString());
        }
    

    只要你需要消息框,就按照这一行

    MsgBox("Your Message!!!", this.Page, this);
    

    【讨论】:

      【解决方案3】:

      您看到的错误是由您的内容页面以某种方式试图注入 javascript 以在 Content 控件之外创建警报框引起的。

      一种可行的方法是在母版页级别注入 javascript。

      为此,请在母版页代码中公开一个方法,如下所示:

      public void ShowAlertMessage(String message)
      {
          string alertScript = String.Format("<Script Language='javascript'> alert('{0}');</script>", message);
          Page.ClientScript.RegisterStartupScript(this.GetType(), "Key", alertScript, false);            
      }
      

      然后,您可以从内容页面中调用 Master 对象上的此方法:

      protected void UpdateProfile_Click(object sender, EventArgs e)
      {
          YourMasterPage master = (YourMasterPage) Master;
          master.ShowMessage("Profile updated.");
      }
      

      此方法还具有为所有内容页面封装 MessageBox 逻辑的好处。


      关于上述内容的一个警告是,我无法终生重现您看到的错误,我已经尝试了所有我能想到的主/内容标记组合,但无法得到错误。其他答案中提供的任何其他示例对我来说都很适用。

      【讨论】:

        【解决方案4】:
         Response.Write("[script] alert('message here');[/script]");
        

        stackoverflow 不允许真正的标签将 [ 替换为

        【讨论】:

        • 如果我在母版页的内容页面中使用 Response.write 方法中的脚本标记..它显示错误“仅允许直接在包含内容控件的内容页面中使用内容控件。”跨度>
        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多