当前位置: 代码迷 >> ASP.NET >> this.ClientScript.RegisterStartupScript(this.GetType(), "alert" "<script langua解决思路
  详细解决方案

this.ClientScript.RegisterStartupScript(this.GetType(), "alert" "<script langua解决思路

热度:1790   发布时间:2013-02-25 00:00:00.0
this.ClientScript.RegisterStartupScript(this.GetType(), "alert", "<script langua
因为做ASP.NET 的增删改,每次都要用到 
this.ClientScript.RegisterStartupScript(this.Page.GetType(), "alert", "<script>alert('删除成功!')</script>");
这条语句,太繁琐了,就想把它作为一个公共类的方法,每次使用时调用一下即可。
不过遇到了小小的问题请求帮助。
公共类:Message
 public ClientScriptManager ClientScript { get; set;}
 public void Alert(string msg)
  {
  this.ClientScript.RegisterStartupScript(this.GetType(), "alert", "<script language='javascript' >alert('" + msg + "');</script>", true);
  }
页面cs代码:
 Message message = new Message();
public void a()
{
 this.message.Alert("删除成功");
}

代码如上,错误是 这一行 this.ClientScript.RegisterStartupScript(this.GetType(), "alert", "<script language='javascript' >alert('" + msg + "');</script>", true); 未将对象引用设置到对象的实例
PS:我在调试时看到msg 已获得值.......

这是什么原因??????????请指教!!!!



------解决方案--------------------------------------------------------
C# code
  private static void OutPutJavaScript(string js)        {            Random rand = new Random(DateTime.Now.Millisecond);            string alert = "alert_" + rand.Next().ToString();            System.Web.UI.Page CurPage = (System.Web.UI.Page)HttpContext.Current.Handler;            System.Web.UI.ClientScriptManager cs = CurPage.ClientScript;            if (!cs.IsClientScriptBlockRegistered(CurPage.GetType(), alert))            {                cs.RegisterClientScriptBlock(CurPage.GetType(), alert, js);            }        }
  相关解决方案