ASP.NET Web应用程序消息框

Bax*_*ter 31 c# asp.net web-applications messagebox code-behind

在asp.net的Windows窗体应用程序中,在C#代码后面你可以使用:

MessageBox.Show("Here is my message");
Run Code Online (Sandbox Code Playgroud)

在asp.net Web应用程序中是否有任何等价物?我可以从后面的C#代码中调用一些可以向用户显示消息框的内容吗?

示例用法:我有一个按钮,可以在后面的代码中加载文件.加载文件或出现错误时,我想向用户弹出一条消息,说明结果.

有什么想法吗?

Gag*_*age 66

您想使用警报.不幸的是,它不如Windows窗体那么好.

ClientScript.RegisterStartupScript(this.GetType(), "myalert", "alert('" + myStringVariable + "');", true);
Run Code Online (Sandbox Code Playgroud)

与此问题类似:http: //forums.asp.net/t/1461308.aspx/1

  • myStringVariable只是一个字符串.它将设置为您要显示的消息. (3认同)

Ali*_*yun 15

或者在解决方案中创建这样的方法:

public static class MessageBox {
    public static void Show(this Page Page, String Message) {
       Page.ClientScript.RegisterStartupScript(
          Page.GetType(),
          "MessageBox",
          "<script language='javascript'>alert('" + Message + "');</script>"
       );
    }
}
Run Code Online (Sandbox Code Playgroud)

然后你就可以使用它:

MessageBox.Show("Here is my message");
Run Code Online (Sandbox Code Playgroud)


Pab*_*tor 10

仅供记录.

这是Microsoft的链接,我认为这是在ASP.Net中呈现MessageBox的最佳方式

它还提供了YesNO等选项.

有关如何从处理项目的链接获取类的说明:

  1. 如果项目中没有App_Code文件夹,请创建它.
  2. 右键单击App_Code文件夹并创建一个Class.将其命名为MessageBox.cs
  3. 复制MessageBox.cs文件中的文本(来自附加的代码)并将其粘贴到MessageBox.cs文件中.
  4. MessageBoxCore.cs文件执行与步骤2和3相同的操作.
  5. 重要说明:右键单击每个文件MessageBox.csMessageBoxCore.cs并确保将"Build Action"设置为Compile
  6. 将此代码添加到要在其中显示消息框的aspx页面:

    <asp:Literal ID="PopupBox" runat="server"></asp:Literal>
    
    Run Code Online (Sandbox Code Playgroud)
  7. 在您要决定的cs页面上添加此代码:

    string title = "My box title goes here";
    string text = "Do you want to Update this record?";
    MessageBox messageBox = new MessageBox(text, title, MessageBox.MessageBoxIcons.Question, MessageBox.MessageBoxButtons.YesOrNo, MessageBox.MessageBoxStyle.StyleA);
    messageBox.SuccessEvent.Add("YesModClick");
    PopupBox.Text = messageBox.Show(this);
    
    Run Code Online (Sandbox Code Playgroud)
  8. 将此方法添加到您的cs页面.这是用户单击"是"时将执行的操作.您不需要为该NoClick方法创建另一个.

    [WebMethod]
    public static string YesModClick(object sender, EventArgs e)
    {
        string strToRtn = "";
        // The code that you want to execute when the user clicked yes goes here
        return strToRtn;
    }
    
    Run Code Online (Sandbox Code Playgroud)
  9. WebUserControl1.ascx文件添加到根路径并将此代码添加到文件中:

    <link href="~/Styles/MessageBox.css" rel="stylesheet" type="text/css" />
    <div id="result"></div>
    <asp:ScriptManager runat="server" ID="scriptManager" EnablePageMethods="True">
    </asp:ScriptManager>  //<-- Make sure you only have one ScriptManager on your aspx page.  Remove the one on your aspx page if you already have one.
    
    Run Code Online (Sandbox Code Playgroud)
  10. aspx页面上添加此行:

    <%@ Register src="~/MessageBoxUserControl.ascx" tagname="MessageBoxUserControl" tagprefix="uc1" %>
    
    Run Code Online (Sandbox Code Playgroud)
  11. aspx页面中添加这一行(如果有的话,在asp:Content标签内)

    <uc1:MessageBoxUserControl ID="MessageBoxUserControl1" runat="server" />
    
    Run Code Online (Sandbox Code Playgroud)
  12. 将上述1.jpg, 2.jpg, 3.jpg, 4.jpgMicrosoft项目中的图像文件保存到~/Images/路径中.

  13. 完成

希望能帮助到你.

巴勃罗