代码改变世界

一个比较实用的服务器端模拟客户端Alert的代码

2008-06-23 17:29  Koy  阅读(493)  评论(1编辑  收藏  举报

public class Alert
{
    public Alert()
    {
       
    }

    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;

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

    public static void Show()
    {
        throw new Exception("The method or operation is not implemented.");
    }
}