简体   繁体   English

一个ajax更新面板中的asp.net gridview控件的linkbutton抛出错误

[英]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. 我在更新面板中有一个gridview。 The grid view has a link button "btnRequest" on every row. 网格视图的每一行上都有一个链接按钮“ btnRequest”。 On clicking the link button , it needs to download a text file on the users desktop. 单击链接按钮时,它需要在用户桌面上下载文本文件。

Here is my code : 这是我的代码:

aspx : 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 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. 从我的研究中,我有点想出Response对象使用HttpHandler并将其放在更新面板中会导致错误。 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. 您的问题是,当您流回文本文件时,UpdatePanel认为恶意内容正在注入其中。 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.). 我一直使用的解决方法是在代码后方构建HTML锚()标记,并将其注入标记中,并使它的href属性指向文件流逻辑(HttpHandler,页面逻辑等)。 That circumvents the UpdatePanel thinking its content is being hacked. 这样就可以绕过UpdatePanel,认为其内容已被黑客入侵。

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. 这是硬编码锚标记的示例,您可以将其放入UpdatePanel中,以调出客户端可访问的地址,在本例中为HttpHandler。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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