Sunday, 23 June 2013

Calling Javascript function to show Message

->Inside App_Code add a class for e.g : Alert.cs
->Add a static method to class

 public static void Show(string message)
        {
            // Cleans the message to allow single quotation marks
            string cleanMessage = message.Replace("'", "\'");
            string script = "<script type=text/javascript>alert('" + cleanMessage + "');</script>";

            // Gets the executing web page
            Page page = HttpContext.Current.CurrentHandler as Page;

            // Checks if the handler is a Page and that the script isn't allready on the Page
            if (page != null && !page.ClientScript.IsClientScriptBlockRegistered("alert"))
            {
                page.ClientScript.RegisterClientScriptBlock(typeof(Alert), "alert", script);
            }
        }

-> calll the method from aspx page like below
   
     Alert.show("Error Message")

No comments:

Post a Comment