[轉]Sys.WebForms.PageRequestManagerParserErrorException的解決方法

 本文轉自:http://www.ximenyifan.com/article/157.html

 

Sys.WebForms.PageRequestManagerParserErrorException的解決方法
在使用UpdatePanel的時候,如果要用到Response.Write()方法,則會出錯,錯誤信息內容大概如下:
Sys.WebForms.PageRequestManagerParserErrorException: The message received from the server could not be parsed. Common causes for this error are when the response is modified by calls to Response.Write(), response filters, HttpModules, or server trace is enabled. Details: Error parsing near ' 你要輸出的內容|UpdatePanel|U'

解決方法如下:

1.如果調用Response.Write()方法的服務器控件在使用UpdatePanel的頁面,則只需要在UpdatePanel下增加一個<Triggers>節點,通過PostBackTrigger註冊一下改控件就可以了。代碼如下:

1. <asp:ScriptManager ID="ScriptManager1" runat="server">
2. </asp:ScriptManager>
3. <asp:UpdatePanel ID="UpdatePanel1" runat="server">
4. <Triggers>
5. <asp:PostBackTrigger ControlID="Button2" /> <!--Button2就是下面那個需要在Button2_Click事件裏使用Response.Write()的按鈕ID-->
6. </Triggers>
7. <ContentTemplate>
8. <asp:Button ID="Button2" runat="server" OnClick="Button2_Click" Text="Button" />
9. <asp:UpdateProgress ID="UpdateProgress1" runat="server">
10. <ProgressTemplate></ProgressTemplate>
11. </asp:UpdateProgress>
12. </ContentTemplate>
13. </asp:UpdatePanel>

2.但是,如果是在母版頁中使用UpdatePanel,則不能通過以上方法來解決,否則或出現類似以下錯誤:

A control with ID 'btnExport' could not be found for the trigger in UpdatePanel 'UpdatePanel1'.

這主要是UpdatePanel1找不到<asp:PostBackTrigger ControlID="btnExport" />中註冊的控件,因爲,我們一般沒有在母版頁中添加這個控件(btnExport)。(當然,如果在UpdatePanel的<ContentTemplate> 節點下添加了ID爲btnExport的控件,則不會出錯。)

如果出現這樣的錯誤該怎麼辦呢,我的解決方法是在需要用到Response.Write()方法的控件所在頁碼的Page_Load事件中添加如下代碼:
((ScriptManager)Master.FindControl("ScriptManager1")).RegisterPostBackControl(btnExport);
//ScriptManager1是<asp:ScriptManager ID="ScriptManager1" runat="server"> </asp:ScriptManager>的ID
這樣,問題就解決了。

下面是我的一個項目中的代碼:
母版頁前臺代碼:

1. <asp:ScriptManager runat="server" ID="ScriptManager1">
2. </asp:ScriptManager>
3. <asp:UpdatePanel ID="UpdatePanel1" runat="server">
4. <ContentTemplate>
5. <asp:ContentPlaceHolder ID="ContentPlaceHolder1" runat="server">
6. </asp:ContentPlaceHolder>
7. <asp:UpdateProgress ID="UpdateProgress1" runat="server">
8. <ProgressTemplate>
9. <table class="progressBox" style="width: 200px">
10. <tr>
11. <td>
12. <asp:Image runat="server" ID="imgLoad" ImageUrl="~/App_Themes/SkinFile/Images/animated_loading.gif" />
13. </td>
14. <td>
15. 正在從服務器下載數據...
16. </td>
17. </tr>
18. </table>
19. </ProgressTemplate>
20. </asp:UpdateProgress>
21. </ContentTemplate>
22. <asp:UpdatePanel>
23.

子頁面後臺代碼:

1. protected void Page_Load(object sender, EventArgs e)
2. {
3. ((ScriptManager)Master.FindControl("ScriptManager1")).RegisterPostBackControl(btnExport);
4. }

這樣,當按鈕btnExport的單擊事件中用到:Response.Write()方法時,就不會出現題目中的錯誤了。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章