在类的开头有这样的定义:
public class Config : System.Web.UI.Pagepublic void ExecuteNonQuery_Sql_alert_refurbish_self(string str_Sql,string alert)
{   
Con.Open();
SqlCommand myCommand = new SqlCommand(str_Sql,Con);
myCommand.ExecuteNonQuery();
myCommand.Connection.Close();
Con.Close();
this.Page.RegisterStartupScript("","<script>alert('"+alert+"');window.location.href=window.location.href;</script>");
}为什么在页面调用Conn.ExecuteNonQuery_Sql_alert_refurbish_self(Sql_input,"添加成功!!!");
不出现对话框和该页刷新

解决方案 »

  1.   

    在编写服务器控件时,经常需要向页面发送大量的客户端代码。一般情况下我们可以直接用output.Write() 写到页面,但这样会带来一个几个问题:
          1、如果同一个页面有几个这样的服务器控件,是不是同样的客户端代码要被写到客户端好几次?
          2、如果让页面自动运行相应的客户端脚本? 
        其实这些问题ASP.NET里已提供了解决方法。
        在Page类里有几个方法:
        RegisterClientScriptBlock      可以向页面中发送客户端代码块(当然可以不仅仅是脚本,也可以是其它的文本),这段代码会被写到Form里的开始部分
        IsClientScriptBlockRegistered   根据一个页面级的Key来判断某段由RegisterClientScriptBlock发送的客户端代码是否已被发送到了客户端。
        RegisterStartupScript    可以向页面中发送客户端代码块,这段代码会被写到Form里的结束部分。这样就可以如其名称一样,在页面加载完成后写入一段自动运行的代码
        IsStartupScriptRegistered  根据一个页面级的Key来判断某段由RegisterStartupScript发送的客户端代码是否已被发送到了客户端
        通过上面的几个方法就可以解决上述的两个问题。   示例:
            //生成客户端脚本        private string GetClientScript()        {            return @"<script language=""javascript"">                        function ClientFunction()                        {                            具体的实现                        }                    </script>";        }         //生成自动运行的客户端脚本        private string GetClientStartUpScript()        {            return @"<script language=""javascript"">                            具体的实现                    </script>";        }         //在预呈现事件里加入客户端脚本        protected override void OnPreRender(EventArgs e)        {            if(!Page.IsClientScriptBlockRegistered("ClientScriptKey1"))                Page.RegisterClientScriptBlock("ClientScriptKey1",GetClientScript());            Page.RegisterStartupScript("ClientScriptKey2",GetClientStartUpScript());            base.OnPreRender(e);        }       ToolTips:
         向页面注册客户端脚本要在预呈现里完成,如果在Redner里来做则不会启作用。
         在写大段的客户端脚本时,可以在文本前加入@,这样,引起来的字符器的换码序列将“不”被处理,这样就可以轻松写出字符串。如果要在字符器里包括一个双引号,请使用两对双引号。
      

  2.   

    在类的开头有这样的定义:
    public class Config : System.Web.UI.Pagepublic void ExecuteNonQuery_Sql_alert_refurbish_self(string str_Sql,string alert)
    {   
    Con.Open();
    SqlCommand myCommand = new SqlCommand(str_Sql,Con);
    myCommand.ExecuteNonQuery();
    myCommand.Connection.Close();
    Con.Close();
    if(!this.Page.IsClientScriptBlockRegistered("alert"))
       this.Page.RegisterStartupScript("alert","<script>alert('"+alert+"');window.location.href=window.location.href;</script>");
    else
    this.Page.RegisterStartupScript("alert1","<script>alert('"+alert+"');window.location.href=window.location.href;</script>"); }为什么我如上定义了判断是否注册还是不行呢!!!
      

  3.   

    在类里面不可以用page.registerstartupscrip语句,因为是类里面没有page类啊即是往特定的网页发角本啊。
      

  4.   

    在类的开头有这样的定义:
    public class Config : System.Web.UI.Pagepublic void ExecuteNonQuery_Sql_alert_refurbish_self(string str_Sql,string alert)
    {   
    Con.Open();
    SqlCommand myCommand = new SqlCommand(str_Sql,Con);
    myCommand.ExecuteNonQuery();
    myCommand.Connection.Close();
    Con.Close();
    if(!this.Page.IsClientScriptBlockRegistered("alert"))
       this.Page.RegisterStartupScript("alert","<script>alert('"+alert+"');window.location.href=window.location.href;</script>");
    else
    this.Page.RegisterStartupScript("alert1","<script>alert('"+alert+"');window.location.href=window.location.href;</script>"); }为什么我如上定义了判断是否注册还是不行呢!!!
      

  5.   

    子类方法中无法直接访问this.Page,可以用下面的两种方法:1. 利用System.Web .HttpContext .Current .Response输出脚本块
       如: System.Web .HttpContext .Current .Response .Write ("<script>alert('hello')</script>");2. 重写Page类中的方法,然后让你的asp.net页面继承这个类。比如:   protected override void OnPreRender(EventArgs e)
        {

    base.OnPreRender (e);
             //可以在这里添加判断是否输出脚本的代码
    base.Page .RegisterStartupScript ("","<script>alert('script')</script>");
        }
      

  6.   

    也可以:
    this.Page .RegisterStartupScript ("","<script>alert('script')</script>");
      

  7.   

    在类中
    public static void MessageBox( Page page, string msg )
            {
             StringBuilder StrScript = new StringBuilder(); 
             StrScript.Append( "<script language=javascript>" );
             StrScript.Append( "alert('"+ msg +"');" ); 
             StrScript.Append( "</script>" ); 
              if ( ! page.IsStartupScriptRegistered( "MessageBox" ) )
             {
             page.RegisterStartupScript( "MessageBox", StrScript.ToString() );
             }
            }
    引用时MessageBox(this,"...");
      

  8.   

    this不就是Page的实例吗?把this.Page的Page删了就行
      

  9.   

    因本人功力有限只理解了 penning(飞鱼) 兄的例子。果然不错的