Javascript Alert in C#


Code:

/// 

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

        }

    }

}

Advertisements

Author: Akhil Mittal

Akhil Mittal is a Microsoft MVP(Most Valuable Professional), C# Corner MVP, Codeproject MVP, a blogger, author and likes to write/read technical articles. Akhil has an experience of around 11 years in developing, designing, architecting enterprises level applications primarily in Microsoft Technologies. Akhil enjoys working on technologies like MVC, Web API, Entity Framework, Angular, C# and BlockChain. Akhil is an MCP( Microsoft Certified Professional) in Web Applications (MCTS-70-528, MCTS-70-515) and .Net Framework 2.0 (MCTS-70-536). Visit Akhil Mittal’s personal blog CodeTeddy (https://codeteddy.com) for some good and informative articles.