【问题标题】:How to show MessageBox on asp.net?如何在 asp.net 上显示 MessageBox?
【发布时间】:2011-05-17 03:37:15
【问题描述】:

如果我需要在我的 ASP.NET WebForm 上显示 MessageBox,该怎么做?

我试试:Messagebox.show("dd");

但它不起作用。

【问题讨论】:

  • 您希望此消息框显示在客户端还是服务器上?您知道 ASP.NET 代码在服务器上运行,对吧?
  • 在 Asp.net 中使用 alert('message') 一点都不专业!它看起来像是一条您无法设置样式的操作系统消息 您应该始终“创建”您自己的模态弹出窗口,也许使用 JQuery 让您的生活更轻松。在网络环境中使用 Alert('') 是一种不好的做法
  • 这能回答你的问题吗? ASP.NET Web Application Message Box

标签: c# asp.net


【解决方案1】:

我从出色的@KrisVanDerMast 中获取代码,并将其封装在一个静态方法中,该方法可以在同一页面上任意多次调用!

/// <summary>
/// Shows a basic MessageBox on the passed in page
/// </summary>
/// <param name="page">The Page object to show the message on</param>
/// <param name="message">The message to show</param>
/// <returns></returns>
public static ShowMessageBox(Page page, string message)
{
    Type cstype = page.GetType();

    // Get a ClientScriptManager reference from the Page class.
    ClientScriptManager cs = page.ClientScript;

    // Find the first unregistered script number
    int ScriptNumber = 0;
    bool ScriptRegistered = false;
    do
    {
        ScriptNumber++;
        ScriptRegistered = cs.IsStartupScriptRegistered(cstype, "PopupScript" + ScriptNumber);
    } while (ScriptRegistered == true);

    //Execute the new script number that we found
    cs.RegisterStartupScript(cstype, "PopupScript" + ScriptNumber, "alert('" + message + "');", true);
}

【讨论】:

    【解决方案2】:

    有一个非常简洁和简单的方法:

    Response.Write("<script>alert('Your text');</script>");
    

    【讨论】:

      【解决方案3】:

      你可以简单地写,但无论如何你都必须使用 JavaScript。

      Page.ClientScript.RegisterStartupScript(Page.GetType(), "Message Box", "<script language = 'javascript'>alert('dd')</script>");
      

      【讨论】:

        【解决方案4】:

        如果需要,您可以使用 MessageBox,但建议使用 alert(来自 JavaScript)。

        如果你想使用它,你应该写:

        System.Windows.Forms.MessageBox.Show("Test");   
        

        请注意,您必须指定命名空间。

        【讨论】:

          【解决方案5】:

          消息框默认仅适用于 windows 窗体应用程序。如果您想使用消息框资源,则必须使用 'using system.windows.forms' 启用 Web 表单模式的消息框。

          【讨论】:

            【解决方案6】:

            Messagebox.show("dd"); 确实不是使用System.Web; 的一部分,

            我大部分时间都感到同样的情况。如果您想这样做,请执行以下步骤。

            • 在解决方案资源管理器中右键单击项目
            • 去添加参考,然后选择.NET选项卡

            • 然后选择,System.windows.forms(按's'快速查找)

            你可以获取命名空间,现在你可以使用Messagebox.show("dd");

            但我建议为此使用 javascript 警报。

            【讨论】:

              【解决方案7】:

              MessageBox 在 ASP.NET 中不存在。如果您需要浏览器中的功能,例如显示消息框,那么您需要选择 javascript。 ASP.NET 为您提供了注入 javascript 的方法,当发送到浏览器的 html 加载和显示时,这些 javascript 会被渲染和执行。例如,您可以在 Page_Load 中使用以下代码:

              Type cstype = this.GetType();
              
              // Get a ClientScriptManager reference from the Page class.
              ClientScriptManager cs = Page.ClientScript;
              
              // Check to see if the startup script is already registered.
              if (!cs.IsStartupScriptRegistered(cstype, "PopupScript"))
              {
                  String cstext = "alert('Hello World');";
                  cs.RegisterStartupScript(cstype, "PopupScript", cstext, true);
              }
              

              此示例的taken from MSDN

              【讨论】:

              • 我的母版页中的代码中有此内容,用于由我的网站页面调用,但我得到的结果好坏参半。有时它会起作用,而其他时候什么也没发生。
              • 请根据您的具体发现开始一个新线程,并提供具有可重现路径的代码,以便人们可以在本地实际测试它并查看问题所在。
              【解决方案8】:

              其中一个选项是使用 Javascript。

              这是一个快速参考,您可以从这里开始。

              Javascript alert messages

              【讨论】:

                【解决方案9】:

                消息框仅适用于 Windows。你必须使用Javascript

                Alert('dd'); 
                

                【讨论】:

                  猜你喜欢
                  • 1970-01-01
                  • 1970-01-01
                  • 1970-01-01
                  • 1970-01-01
                  • 1970-01-01
                  • 1970-01-01
                  • 2022-01-06
                  • 1970-01-01
                  • 1970-01-01
                  相关资源
                  最近更新 更多