日期:2014-05-17  浏览次数:20503 次

App_Code下的类中运用ClientScriptManager弹出窗口
C# code
public class Class1 : System.Web.UI.Page
{
    public Class1()
    {
    }

    public void aa(string strMessage)
    {
        ClientScriptManager cs = Page.ClientScript;
        Type cstype = this.GetType();
        string csstring = "PopupScript";
        string cstext = "alert('" + strMessage + "');";
        cs.RegisterStartupScript(cstype, csstring, cstext, true);
    }
}

如上边的代码,我想将alert("....")写到一个类中然后再某些页面需要探出提示窗口的时候直接调用,但是在执行的时候没有出现问题,只是不会出现么messagebox
我想请问一下是不是Page.ClientScript的问题,个位帮忙解答一下!

------解决方案--------------------
类a
public static void Alert(string str, Page page)
{
str = Regex.Replace(str,"['|\r|\n]*", "");
page.ClientScript.RegisterStartupScript(page.GetType(), "alert", string.Format("alert(\"{0}\")", str), true);
}
调用:
a.Alert("xxx", Page);
------解决方案--------------------
this.Page.RegisterStartupScript("alert", "<script language=javascript>alert('" + strMessage + "');</script>");
用这个 传一个page对象进去试试
我没用 只是猜想
------解决方案--------------------
C# code
public class Class1{
    public Class1()
    {
    }

    public void aa(string strMessage)
    {
        Page page = (Page)System.Web.HttpContext.Current.Handler;
        ClientScriptManager cs = page.ClientScript;
        Type cstype = this.GetType();
        string csstring = "PopupScript";
        string cstext = "alert('" + strMessage + "');";
        cs.RegisterStartupScript(cstype, csstring, cstext, true);
    }
}

------解决方案--------------------
C# code
        /// <summary>
        /// 警告框
        /// </summary>
        /// <param name="_Msg">警告字串</param>
        /// <param name="_Page">this</param>
        /// <returns>警告框JS</returns>
        public static object Alert(string _Msg, Page _Page)
        {
            string StrScript;
            StrScript = ("<script language=javascript>");
            StrScript += ("alert('" + _Msg + "');");
            StrScript += ("</script>");
            _Page.ClientScript.RegisterStartupScript(_Page.GetType(), "MsgBox", StrScript.ToString());
            return StrScript;
        }

------解决方案--------------------
C# code
public void aa(string strMessage)
    {
         string cstext = "alert('" + strMessage + "');";
         ((Page)HttpContext.Current.Handler).ClientScript.RegisterStartupScript(this.GetType(), Guid.NewGuid().ToString(), cstext, true);
    }