Javascript Alert in C#

Posted by



/// A JavaScript alert 


public static class Alert



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

    /// The message to appear in the alert.

    public static void Show(string message)


        // Cleans the message to allow single quotation marks

        string cleanMessage = message.Replace(“‘”“\\'”);
        string script = string.Format(“$(document).ready(function(){alert(‘{0}’);});”, cleanMessage);

// 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 all ready on the Page

        if (page != null && !page.ClientScript.IsClientScriptBlockRegistered(“alert”))


            page.ClientScript.RegisterClientScriptBlock(typeof(Alert), “alert”, script);




Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.