简体   繁体   中英

linkbutton of an asp.net gridview control within an ajax update panel throwing error

I get the following error.

    JavaScript runtime error: Sys.WebForms.PageRequestManagerParserErrorException: The message received 
from the server could not be parsed.

I tried debugging it, including looking for help online, but I could not resolve the issue yet. Here is what I have :

I have a gridview inside an update panel. The grid view has a link button "btnRequest" on every row. On clicking the link button , it needs to download a text file on the users desktop.

Here is my code :

aspx :

<div class="ModalPopup" id="ViewModalPopupDiv1">
        <asp:UpdatePanel ID="UpdatePanel2" runat="server">
            <ContentTemplate>
                <table>
                    <tr>
                        <td>
                            <div class="modalHeader">
                                <table width="100%">
                                    <tr>
                                        <td class="title">
                                            <asp:Label ID="Label2" runat="server" Text="WebServiceCall Details" Font-Bold="true" ></asp:Label>
                                        </td>
                                        <td>
                                            <a href="javascript:void(0);" onclick="javascript:CloseModelPopup1();" class="CloseModal">
                                                X</a>
                                        </td>
                                    </tr>
                                </table>
                            </div>
                        </td>
                    </tr>
                    <tr>
                        <td>
                            <div id="Div1" class="InsertBar">
                                <asp:Panel ID="Panel1" runat="server" HorizontalAlign="left" ScrollBars="Auto">
                                    <asp:GridView ID="gvDetails" OnRowDataBound="gvDetails_RowDataBound"
                                        OnRowCommand="gvDetails_RowCommand" DataKeyNames="Name"
                                        runat="server" CellPadding="5" AutoGenerateColumns="false">
                                        <Columns>
                                            <asp:TemplateField>
                                                <ItemTemplate>
                                                    <asp:Image Width="32px" ID="statusImage" runat="server" Height="32px"></asp:Image>
                                                </ItemTemplate>
                                            </asp:TemplateField>
                                            <asp:TemplateField HeaderText="LogId">
                                                <ItemTemplate>
                                                                                                            <asp:LinkButton CommandName="DownloadTextFile" Text='<%# Bind("LogId") %>'
                                    CommandArgument='<%# Container.DataItemIndex %>' runat="server" ID="btnRequest"></asp:LinkButton>
                                                </ItemTemplate>
                                            </asp:TemplateField>
                                        </Columns>
                                    </asp:GridView>
                                </asp:Panel>
                            </div>
                        </td>
                    </tr>
                </table>
            </ContentTemplate>
        </asp:UpdatePanel>
    </div> 

aspx.cs

protected void gvDetails_RowCommand(object sender, GridViewCommandEventArgs e)
        {


            if (e.CommandName.ToLower() == "DownloadTextFile")
            {
                int index;
                index = Convert.ToInt32(e.CommandArgument);

                object objTemp = gvDetails.DataKeys[index].Values[1].ToString();

                string Request;

                string fileName = string.Empty;

                if (objTemp != null)
                {
                    Request = XMLHelper.IndentXmlString(GetLogRequestByName((objTemp.ToString())));


                    StringBuilder sb = new StringBuilder();
                    sb.AppendLine(Request + "\r\n");
                    fileName = "log" + "_" + objTemp.ToString();

                    Response.Clear();
                    Response.Buffer = true;
                    Response.AddHeader("content-disposition", "attachment;filename=" + fileName + ".txt");
                    Response.Charset = "";
                    Response.ContentType = "text/plain";
                    Response.Output.Write(sb.ToString());
                    Response.Flush();
                    Response.End();
                }
            }
        }

From my research , I kind of figured out that the Response object uses the HttpHandler and having this within the update panel is causing the error. But I could not figure out on how to resolve the problem . Please let me know if you have any points that can help me resolve the problem

Your issue is that the UpdatePanel thinks that malicious content is being injected into it when you stream back the text file. The workaround that I have always used is to build an HTML anchor () tag, in code-behind and inject it into the markup, and make it's href attribute point to the file streaming logic (HttpHandler, page logic, etc.). That circumvents the UpdatePanel thinking its content is being hacked.

Here is a sample of a hardcoded anchor tag you could put in your UpdatePanel that would call out to a client reachable address, in this case an HttpHandler.

<a href='RelativePathTo/Handlers/DownloadText.ashx?id=7'>Download Text</a>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM