Skip to content

How to display message box if any error occur in the page using c sharp?

MohanRaj C

Asked Jun 18, 2012, 4:14 AM EDT1 answer
Share
LinkedInX
How to display message box if any error occur in the page using c sharp?

If any error occur in the page during submitting the form,

i need to display message box.....

I tried this code in Exception

Response.Write("<script>alert('" + ex + "')</script>");

but it dindt work .....

How i solve this problem?

1 Answer

Know the answer? Post it. Somebody with the same question will find it here.

  • Gohil Jayendrasinh
    0
    Hi



    public class Alert



        {



            /// <summary>



            /// Shows a client-side
    JavaScript alert in the browser.



            /// </summary>



            /// <param name="message">The message to appear in the alert.</param>



            public static
    void Show(string message)



            {



                // Cleans the message to allow single quotation marks



    Code
                string
    cleanMessage = message.Replace("'",
    "\\'");


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



                }



            }   



        }

    0