BeanSoftware Logo
 

 
ASP.NET Database Search Control
 
 

 
 Home
    Shop
    Advertise
    Write For Us
    Affiliate
    Newsletter
    Contact

How To Show Message Box?

To show JavaScript message box by using ASP.NET server side code you basically need to add client script and use alert JavaScript function. There are different implementations of this, but one of the best is to add ASP.NET folder App_Code and then add new MessageBox class in App_Code folder. The code for MessageBox class is:

[ C# ]

using System.Web.UI;
 
 
/// <summary>
/// ASP.NET JavaScript message box class
/// </summary>
public static class MessageBox
{
    public static void ShowMessage(string MessageText, Page MyPage)
    {
        MyPage.ClientScript.RegisterStartupScript(MyPage.GetType(),
            "MessageBox", "alert('" + MessageText.Replace("'", "\'") + "');", true);
    }
}

[ VB.NET ]

Imports System.Web.UI
 
 
''' <summary>
''' ASP.NET JavaScript message box class
''' </summary>
Public Static Class MessageBox
 
    Public Static Sub ShowMessage(ByVal MessageText As String, ByVal MyPage As page)
 
        MyPage.ClientScript.RegisterStartupScript(MyPage.GetType(), _
            "MessageBox", "alert('" & MessageText.Replace("'", "\'") & "');", True)
    End Sub
End Class

 

After that, you can call this MessageBox class from any page with code like this:

[ C# ]

protected void Page_Load(object sender, EventArgs e)
{
    MessageBox.ShowMessage("This is a message!", this.Page);
}

[ VB.NET ]

Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
  MessageBox.ShowMessage("This is a message!", Me.Page)
End Sub

 

Note that if you use ASP.NET 1.1 with Visual Studio 2003 you don't have App_Code folder. In that case you can show message box with simple code like this:

[ C# ]

Response.Write("<SCRIPT>alert('This is a message')</SCRIPT>");

[ VB.NET ]

Response.Write("<SCRIPT>alert('This is a message')</SCRIPT>")



Related articles:

1. How To Get Web Site Thumbnail Image In ASP.NET

FAQ toolbar: Submit FAQ  |  Tell A Friend  |  Add to favorites  |  Feedback



Copyright © 2002-2008 Bean Software. All rights reserved.