Ajax中调用javaScript

原文地址:http: // www.cnblogs.com/carekee/articles/1740124.html

因为用的是Ajax的  ScriptManager + UpdatePanel控件

所以调用Response.Write(),Page.RegisterStartScript()等方法,就不行了

需要调用Ajax本身的ScriptManager.RegisterStartupScript()方法;

具体格式为:

System.Web.UI.ScriptManager.RegisterStartupScript(Control control, Type type,  string key, string script,  bool addScriptTags)

调用方式为:

System.Web.UI.ScriptManager.RegisterStartupScript( this, this.GetType(), " KEY ", " alert('ss') ", true);

 

现在项目中用到的列出几个,以后可能用到的:

AjaxFunc.cs

 

///   <summary>
///  MSG 的摘要说明
///   </summary>
public  static  class AjaxFunc
{
     ///   <summary>
    
///  弹出对话框
    
///   </summary>
    
///   <param name="ps"></param>
    
///   <param name="strName"></param>
    
///   <param name="strMsg"></param>
     public  static  void MessageBox(Page ps,  string strName,  string strMsg)
    {
        ScriptManager.RegisterClientScriptBlock(ps, ps.GetType(), strName,  " alert(' " + strMsg +  " ') "true);
    }

     ///   <summary>
    
///  加载网页
    
///   </summary>
    
///   <param name="ps"></param>
    
///   <param name="strName"></param>
    
///   <param name="strUrl"></param>
     public  static  void OpenUrl(Page ps, string strName, string strUrl)
    {
        System.Web.UI.ScriptManager.RegisterStartupScript(ps, ps.GetType(), strName,  " window.location.href=' " + strUrl +  " '; "true);
    }

     ///   <summary>
    
///  弹出网页
    
///   </summary>
    
///   <param name="ps"></param>
    
///   <param name="strName"></param>
    
///   <param name="strUrl"></param>
     public  static  void OpenModelUrl(Page ps, string strName, string strUrl)
    {
         // System.Web.UI.ScriptManager.RegisterStartupScript(ps, ps.GetType(), strName, "window.showModalDialog ('" + strUrl + "', 'newwindow', 'height=100, width=400, toolbar=no, menubar=no, scrollbars=no, resizable=no, location=no, status=no');", true);
        System.Web.UI.ScriptManager.RegisterStartupScript(ps, ps.GetType(), strName,  " window.showModalDialog (' " + strUrl +  " ', 'newwindow', 'dialogHeight:650px, dialogWidth:300px, toolbar=no, menubar=no, resizable=no, location=no, status=no'); "true);
    }

     ///   <summary>
    
///  弹出网页
    
///   </summary>
    
///   <param name="ps"></param>
    
///   <param name="strName"></param>
    
///   <param name="strUrl"></param>
     public  static  void OpenModelUrl(Page ps,  string strName,  string strUrl, int width,  int height)
    {
         // System.Web.UI.ScriptManager.RegisterStartupScript(ps, ps.GetType(), strName, "window.showModalDialog ('" + strUrl + "', 'newwindow', 'height=100, width=400, toolbar=no, menubar=no, scrollbars=no, resizable=no, location=no, status=no');", true);
        System.Web.UI.ScriptManager.RegisterStartupScript(ps, ps.GetType(), strName,  " window.showModalDialog (' " + strUrl +  " ', 'newwindow', 'dialogHeight: " + height.ToString() +  " px; dialogWidth: " + width.ToString() +  " px; toolbar:no; resizable:no; location:no; status:no;help:no;scroll:no;'); "true);
    }

     ///   <summary>
    
///  弹出网页+状态栏
    
///   </summary>
    
///   <param name="ps"></param>
    
///   <param name="strName"></param>
    
///   <param name="strUrl"></param>
     public  static  void OpenModelUrl_EX(Page ps,  string strName,  string strUrl,  int width,  int height)
    {
         // System.Web.UI.ScriptManager.RegisterStartupScript(ps, ps.GetType(), strName, "window.showModalDialog ('" + strUrl + "', 'newwindow', 'height=100, width=400, toolbar=no, menubar=no, scrollbars=no, resizable=no, location=no, status=no');", true);
        System.Web.UI.ScriptManager.RegisterStartupScript(ps, ps.GetType(), strName,  " window.showModalDialog (' " + strUrl +  " ', 'newwindow', 'dialogHeight: " + height.ToString() +  " px; dialogWidth: " + width.ToString() +  " px; toolbar:no; resizable:no; location:no; status:yes;help:no;scroll:no;'); "true);
    }

     ///   <summary>
    
///  弹出网页
    
///   </summary>
    
///   <param name="ps"></param>
    
///   <param name="strName"></param>
    
///   <param name="strUrl"></param>
     public  static  void OpenModelUrl_DW(Page ps,  string strName,  string strUrl)
    {
         // System.Web.UI.ScriptManager.RegisterStartupScript(ps, ps.GetType(), strName, "window.showModalDialog ('" + strUrl + "', 'newwindow', 'height=100, width=400, toolbar=no, menubar=no, scrollbars=no, resizable=no, location=no, status=no');", true);
        System.Web.UI.ScriptManager.RegisterStartupScript(ps, ps.GetType(), strName,  " window.showModalDialog (' " + strUrl +  " ', 'newwindow', 'dialogHeight:700px, dialogWidth:300px, toolbar=no, menubar=no, resizable=no, location=no, status=no'); "true);
    }
     ///   <summary>
    
///  关闭模式网页
    
///   </summary>
    
///   <param name="ps"></param>
    public  static  void CloseModalUrl(Page ps)
    {
        System.Web.UI.ScriptManager.RegisterStartupScript(ps, ps.GetType(),  """ window.close(); "true);
    }

     ///   <summary>
    
///  使用AJAX操作Response.Write
    
///   </summary>
    
///   <param name="control"></param>
    
///   <param name="type"></param>
    
///   <param name="key"></param>
    
///   <param name="script"></param>
    
///   <param name="addScriptTags"></param>
    public  static  void ResponseWrite(Control control, Type type,  string script,  bool addScriptTags)
    {
        System.Web.UI.ScriptManager.RegisterStartupScript(control, type,  " NULL ", script, addScriptTags);
    }

 

方法的调用:

AjaxFunc.ResponseWrite( thisthis.GetType(),  " alert('请登录后再使用该系统!');parent.document.location.href('../default.aspx'); "true);

JS解释:

alert( ' 请登录后再使用该系统! ');弹出消息框

parent.document.location.href( ' ../default.aspx ');打开指定页

 

AjaxFunc.MessageBox( this" Error "" 输入旧密码不能为空! ");

JS解释:弹出消息框

 

AjaxFunc.OpenModelUrl_EX( this.Page,  """ ShowCurveLine.aspx "840550);

JS解释:打开模式页面

 

AjaxFunc.OpenModelUrl_EX( this.Page,  """ ShowCurveLine.aspx "840550);

JS解释:打开非模式页面

转载于:https://www.cnblogs.com/xidongs/archive/2011/12/28/2304684.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值