ASP.NET Web Application Message Box ASP.NET Web Application Message Box asp.net asp.net

ASP.NET Web Application Message Box


You want to use an Alert. Unfortunately it's not as nice as with windows forms.

ClientScript.RegisterStartupScript(this.GetType(), "myalert", "alert('" + myStringVariable + "');", true);

Similar to this question here: http://forums.asp.net/t/1461308.aspx/1


Or create a method like this in your solution:

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

Then you can use it like:

MessageBox.Show("Here is my message");


Here is a link from Microsoft that I think is the best way to present a MessageBox in ASP.NET

Also it presents choices like Yes and No.

Instructions on how to get the class from the link working on your project:

  1. If you don't have an App_Code folder on your Project, create it.

  2. Right click the App_Code folder and create a Class. Name it MessageBox.cs

  3. Copy the text from the MessageBox.cs file (from the attached code) and paste it on your MessageBox.cs file.

  4. Do the same as steps 2 & 3 for the MessageBoxCore.cs file.

  5. Important: Right click each file MessageBox.cs and MessageBoxCore.cs and make sure the 'Build Action' is set to Compile

  6. Add this code to your aspx page where you want to display the message box:

     <asp:Literal ID="PopupBox" runat="server"></asp:Literal>
  7. Add this code on you cs page where you want to decision to be made:

     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);
  8. Add this method to your cs page. This is what will be executed when the user clicks Yes. You don't need to make another one for the NoClick method.

     [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; }
  9. Add a WebUserControl1.ascx file to your root path and add this code to the file:

     <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.
  10. Add this line on top of your aspx page:

    <%@ Register src="~/MessageBoxUserControl.ascx" tagname="MessageBoxUserControl" tagprefix="uc1" %>
  11. Add this line inside your aspx page (Inside your asp:Content tag if you have one)

    <uc1:MessageBoxUserControl ID="MessageBoxUserControl1" runat="server" />
  12. Save the image files 1.jpg, 2.jpg, 3.jpg, 4.jpg from the Microsoft project above into your ~/Images/ path.