Monday, January 18, 2010

Displaying Warning Messages in ASP.NET page- II

Displaying Web Warning Messages: Technique 2
There are times when you want to draw attention to an important message, without being overly loud. Sometimes, all you want to say is... "Thank you! Your information has been stored" or "Finished: all your outstanding reviews have been completed."
In these situations, a message box is a little overkill. Slightly more placid is the technique of displaying a whole Web page with just your core message at its center. I also prefer to add automatic redirects on such pages, so that after a few seconds, the user gets taken back to the previous page, or through to the next.
There are two ways to achieve this goal. First, create a separate page in your application that accepts a message in its query string, and then send your user across to that page.
Alternatively, you can do it wholly in code—which is just what we're doing here. I've created a small method holding a core HTML Web page, which you could perhaps load from a file. Inside that page, we have numerous variables (such as a message description) that get replaced by the method. Then, we push the modified HTML down the wire as a response to our client.
The HTML also contains a HTTP Refresh command, which by default takes the user back to the previous page. So, in brief: When you call the function, it displays a message for a number of seconds, and then returns to the issuing page.
Here's the function you'll need:
public void DisplayMessage(string MsgTitle, string MsgDetails, string PageTitle, int DelayInSeconds)
    {
      PageTitle = "Attention!";
      DelayInSeconds = 2;
      string strResponse = "<html><head><title>%page-title%</title><META HTTP-EQUIV='Refresh' "  
        "CONTENT='%delay%; url=javascript:history.back();'>"  
        "</head><body><div align='center'><center>"  
        "<table border='0'cellpadding='0' cellspacing='0' width='100%' height='100%'><tr> "  
        "<td width='100%'> <p align='center'><b> <font face='Arial' size='6'>%message-title%"  
        "</font></b></p><p align='center'><font face='Arial' size='3'><b>%message-details%</b>"  
        "</font></td></tr></table></center></div></body></html>";

      strResponse = strResponse.Replace("%page-title%", PageTitle);
      strResponse = strResponse.Replace("%message-title%", MsgTitle);
      strResponse = strResponse.Replace("%message-details%", MsgDetails);
strResponse = strResponse.Replace("%delay%",   DelayInSeconds.ToString);

      Response.Clear();
      Response.Write(strResponse);
      Response.End();
    }
protected void Button1_Click(object sender, EventArgs e)
{
DisplayMessage("Thanks", "You are being redirected now", 
"Thanks!!!", 3);
}

No comments:

Post a Comment