Showing posts with label javascript. Show all posts
Showing posts with label javascript. Show all posts

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

Displaying Warning Messages in ASP.NET pages- I

Thought of a situation when you need to call javascript alert from your server side code that might include some server side decision logic to handle the alert. Most of us know a straight way- "Page.ClientScript.RegisterStartupScript" method to accomplish this. Althoght accidentally, I came to know another approach to do the same without -   "Page.ClientScript.RegisterStartupScript".

Switch to the HTML view of your Web form and add the following immediately after the close of the <body> tag:
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
    <title>Untitled Page</title>
</head>
<body>

</body>
<script>
<asp:Literal id="ltlAlert" runat="server" EnableViewState="False">    </asp:Literal>
 </script>
</html>
Switch back to Design view and save your Web form. This has set up your Literal server control manually—due to the surrounding <script> tags; we couldn't do this using the designer
Add the following code snippet behind your Web form. This takes a string and incorporates it into a JavaScript 'alert' command, which is then placed on the Web page as pure HTML:
private void Say(string Message)
    {
        // Format string properly 
        Message = Message.Replace("'", "\\'");
        Message = Message.Replace(Convert.ToChar(10).ToString(), "\\n");
        Message = Message.Replace(Convert.ToChar(13).ToString(), "");
        // Display as JavaScript alert 
        ltlAlert.Text = "alert('" + Message + "')";
    }
Whenever you want to display an in-your-face message, simply call this Say function in your code—as the following snippet demonstrates:
protected void Page_Load(object sender, EventArgs e)
    {
        Say("this is error msg\n next line");
    }
Thats it!!