用到ScriptManager的頁面後台使用彈出對話方塊

來源:互聯網
上載者:User
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解釋:開啟非模式頁面

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.