In this article I will explain with an example, why MessageBox.Show does not work i.e. does not display messages in
ASP.Net.
ASP.Net is a Client Server technology where the program runs on Server while Page is displayed on Client in a Browser, while MessageBox.Show function is a
Windows Forms function and hence when used in
ASP.Net it displays Message Box on Server.
Thus in
ASP.Net, the messages needs to be displayed with the help of
JavaScript Alert Message Box where
JavaScript is a Client Side script which runs within Browser.
Display Message Box when user visits the page
HTML Markup
Button – For displaying Alert Message Box when user visit the page.
<asp:Button ID="btnAlert" Text="Button" runat="server" OnClick="ShowAlert" />
Code Behind
Inside the
ShowAlert event handler, the
JavaScript function will be registered using the
RegisterClientScriptBlock function. The below
JavaScript function will display a greeting message to the user when the user
visits the page using
JavaScript Alert Message Box.
Note: The
JavaScript Alert function is called inside the
window.onload function so that it is displayed after the loading of the
Page in Browser.
C#
protected void ShowAlert(object sender, EventArgs e)
{
string message = "Hello! Mudassar.";
System.Text.StringBuilder sb = new System.Text.StringBuilder();
sb.Append("<script type = 'text/javascript'>");
sb.Append("window.onload=function(){");
sb.Append("alert('");
sb.Append(message);
sb.Append("')};");
sb.Append("</script>");
ClientScript.RegisterClientScriptBlock(this.GetType(), "alert", sb.ToString());
}
VB.Net
Protected Sub ShowAlert(sender As Object, e As EventArgs)
Dim message As String = "Hello! Mudassar."
Dim sb As New System.Text.StringBuilder()
sb.Append("<script type = 'text/javascript'>")
sb.Append("window.onload=function(){")
sb.Append("alert('")
sb.Append(message)
sb.Append("')};")
sb.Append("</script>")
ClientScript.RegisterClientScriptBlock(Me.GetType(), "alert", sb.ToString())
End Sub
Display Message Box when user submits the page
There are two types of Message Boxes which can be displayed when the User submits the Form.
1. Notification Message Box.
2. Confirmation Message Box.
Notification Message Box on Form Submission
HTML Markup
Button – For displaying Notification Message Box.
<asp:Button ID="btnNotification" Text="Notification" runat="server" OnClick="ShowNotification" />
Inside the
ShowNotification event handler, the
JavaScript function will be registered using the
RegisterOnSubmitStatement function. The below
JavaScript function will display a message to the
user of
Notification on Form
Submission using
JavaScript Alert Message Box.
C#
protected void ShowNotification(object sender, EventArgs e)
{
string message = "Your request is being processed.";
System.Text.StringBuilder sb = new System.Text.StringBuilder();
sb.Append("alert('");
sb.Append(message);
sb.Append("');");
ClientScript.RegisterOnSubmitStatement(this.GetType(), "alert", sb.ToString());
}
VB.Net
Protected Sub ShowNotification(sender As Object, e As EventArgs)
Dim message As String = "Your request is being processed."
Dim sb As New System.Text.StringBuilder()
sb.Append("alert('")
sb.Append(message)
sb.Append("');")
ClientScript.RegisterOnSubmitStatement(Me.GetType(), "alert", sb.ToString())
End Sub
Confirmation Message Box on Form Submission
HTML Markup
Button – For displaying Confirmation Message Box.
<asp:Button ID="btnConfirmation" Text="Confirmation" runat="server" OnClick="ShowConfirmation" />
Inside the
ShowConfirmation event handler, the
JavaScript function will be registered using the
RegisterOnSubmitStatement function. The below
JavaScript function will display a
JavaScript Confirmation Message Box to the user when the user
submits the Form.
C#
protected void ShowConfirmation(object sender, EventArgs e)
{
string message = "Do you want to Submit?";
System.Text.StringBuilder sb = new System.Text.StringBuilder();
sb.Append("return confirm('");
sb.Append(message);
sb.Append("');");
ClientScript.RegisterOnSubmitStatement(this.GetType(), "alert", sb.ToString());
}
VB.Net
Protected Sub ShowConfirmation(sender As Object, e As EventArgs)
Dim message As String = "Do you want to Submit?"
Dim sb As New System.Text.StringBuilder()
sb.Append("return confirm('")
sb.Append(message)
sb.Append("');")
ClientScript.RegisterOnSubmitStatement(Me.GetType(), "alert", sb.ToString())
End Sub
Displaying Alert Message Box on Button Click
HTML Markup
Button – For displaying Alert Message Box.
<asp:Button ID="btnPlaceOrder" Text="Place Order" runat="server" OnClick="OnOrderPlace" />
Code Behind
Inside the
OnOrderPlace event handler, the
JavaScript function will be registered using the
RegisterClientScriptBlock function. The below
JavaScript function will display a
JavaScript Alert Message Box to the user when he clicks the
Button. For example, displaying a message to the user saying that
“Email Sent Successfully” or
“Order Placed Successfully”.
C#
protected void OnOrderPlace(object sender, EventArgs e)
{
string message = "Order Placed Successfully.";
System.Text.StringBuilder sb = new System.Text.StringBuilder();
sb.Append("<script type = 'text/javascript'>");
sb.Append("window.onload=function(){");
sb.Append("alert('");
sb.Append(message);
sb.Append("')};");
sb.Append("</script>");
ClientScript.RegisterClientScriptBlock(this.GetType(), "alert", sb.ToString());
}
VB.Net
Protected Sub OnOrderPlace(sender As Object, e As EventArgs)
Dim message As String = "Order Placed Successfully."
Dim sb As New System.Text.StringBuilder()
sb.Append("<script type = 'text/javascript'>")
sb.Append("window.onload=function(){")
sb.Append("alert('")
sb.Append(message)
sb.Append("')};")
sb.Append("</script>")
ClientScript.RegisterClientScriptBlock(Me.GetType(), "alert", sb.ToString())
End Sub
Browser Compatibility
* All browser logos displayed above are property of their respective owners.
Downloads