Tuesday, April 24, 2012

Display Message when user visits the page


The script below will display the greeting message to the user when the user visits the page. As you will notice I have attached the script on to the window onload event of the page.
C#
protected void Page_Load(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 Page_Load(ByVal sender As ObjectByVal e As System.EventArgs) Handles Me.Load
    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



The figure below displays the message box that is being shown to the user.

Notification MessageBox in ASP.Net

Many places I have seen people using the script in the following way


string script = "<script type = 'text/javascript'>alert('Hello');</script>";

The above will display the message correctly but the message will be displayed even before the page is loaded completely and will stop the execution until user closes the message box. Hence I have called the script on the onload event of the page which will fire the alert only when the page is loaded completely.


Display Message when user submits the page

You can also display a message box to user when he submits the page using RegisterOnSubmitStatement function. You can display two types of messages
1. Notification message.
2. Confirmation message.
First we will see how to display notification message

Notification Message on Form Submission
The code snippet below displays a notification message when the submit button is pressed.
C#
protected void Page_Load(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 Page_Load(ByVal sender As ObjectByVal e As System.EventArgs) Handles Me.Load
     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 on Form Submission
      
The code snippet below displays a confirmation box to the user with OK and Cancel buttons. If user presses OK form is submitted else not.

C#
protected void Page_Load(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 Page_Load(ByVal sender As ObjectByVal e As System.EventArgs) Handles Me.Load
   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

The figure below displays a confirmation message

Confirmation MessageBox in ASP.Net



Display Message Box on Button Click

The code snippet below executes the script after some function is executed on button click. This can be used to provide notifications to user after some operation has been performed. For example you can display a message to the user saying that “Email Sent Successfully” or “Order Placed Successfully” to the user.

C#
protected void Button1_Click(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 Button1_Click(ByVal sender As ObjectByVal e As System.EventArgs) HandlesButton1.Click
  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


The above code has been tested in the following browsers

Internet Explorer  FireFox  Chrome  Safari  Opera 
* All browser logos displayed above are property of their respective owners.

No comments:

Post a Comment