Sys.WebForms.PageRequestManagerErrorException:The message received from the server could not be parsed錯誤解決方案
因為用的是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解釋:開啟非模式頁面