因为用的是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(this, this.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", 840, 550);
JS解释:打开模式页面
AjaxFunc.OpenModelUrl_EX(this.Page, "", "ShowCurveLine.aspx", 840, 550);
JS解释:打开非模式页面