0

I am currently working on VS 2010. I have a gridview on my content page. I am trying to download a file from the server.

The file downloading functionality happens on the click of a link button which is placed within the gridview for each individual record. The gridview is placed within an update panel and the Script Manager is placed on the master page. Also, i have made use of bootstrap on my page.

On the click of the link button withing the gridview the following error is displayed

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

I have searched the internet for solutions but could not find any fix. I came across links where the buttons where placed outside the grid and a similar issue was faced.But, did not find it helpful.

I have tried with using a "PostBackTrigger". But that does not fix the issue. I have referred the below link but it does not provide a solution to the issue suggested above

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

I have also referred other links but could not find a fix to the problem. Also removing the Update Panel is not an option.

I am placing the related design and code of my page.

Design -

<div class="col-xs-12 form-group">
                <div class="table-responsive">
                    <asp:UpdatePanel ID="updPnlErrorDownload" runat="server" UpdateMode="Conditional">
                    <%--<Triggers>
                        <asp:AsyncPostBackTrigger  ControlID="lnkLogFiles"/>
                    </Triggers>--%>
                        <ContentTemplate>
                            <asp:GridView ID="gvLogFilesDownload" runat="server"
                                AutoGenerateColumns="false" AllowPaging="true" 
                                EmptyDataText="No Data Found" Width="100%" 
                                CssClass="table table-striped table-bordered table-hover" PageSize="10" 
                                onpageindexchanged="gvLogFilesDownload_PageIndexChanged" 
                                onrowcommand="gvLogFilesDownload_RowCommand" 
                                onrowcreated="gvLogFilesDownload_RowCreated">
                                <Columns>
                                    <asp:TemplateField HeaderText="Date">
                                        <ItemTemplate>
                                            <asp:Label ID="lblDate" runat="server" Text='<%#Eval("Date") %>' />
                                        </ItemTemplate>
                                    </asp:TemplateField>
                                    <asp:TemplateField HeaderText="Log Files">
                                        <ItemTemplate>
                                            <asp:LinkButton ID="lnkLogFiles" runat="server" Text='<%#Eval("Log Files") %>' Font-Underline="true" CommandName="Download" CommandArgument="<%#((GridViewRow)Container).RowIndex %>"  OnClick="lnkLogFiles_Click"/>
                                        </ItemTemplate>
                                    </asp:TemplateField>
                                </Columns>
                                <EmptyDataTemplate>
                                    No Record Available</EmptyDataTemplate>
                            </asp:GridView>
                        </ContentTemplate>
                        <Triggers>
                        <%--<asp:PostBackTrigger ControlID="lnkLogFiles" />--%>
                       <%-- <asp:PostBackTrigger ControlID="gvLogFilesDownload$lnkLogFiles" />--%>
                    </Triggers>
                    </asp:UpdatePanel>
                </div>
            </div>

Code -

protected void lnkLogFiles_Click(object sender, EventArgs e)
{
    LinkButton lnkBtnDownload = sender as LinkButton;
    string file = lnkBtnDownload.Text;
    //string sPath1 = Server.MapPath(file);
    string sPath = Server.MapPath("~/ErrorLog/" + file);
    //Response.ContentType = "APPLICATION/OCTET-STREAM";
    Response.ContentType = "APPLICATION/pdf";
    Response.AppendHeader("Content-Disposition", "attachment;filename=" + System.IO.Path.GetFileName(sPath));
    Response.TransmitFile(sPath);
    Response.End();
}

Any advice / suggestions is greatly appreciated. Thanks in advance.

user71934
  • 3
  • 4
  • If you are sending a file to the browser from within an UpdatePanel, then you need to make sure that `lnkLogFiles` are PostBack triggers. – VDWWD Jan 19 '18 at 12:07
  • Hi VDWWD, I have tried doing it. But it throws an error - A control with ID 'lnkLogFiles' could not be found for the trigger in UpdatePanel 'updPnlErrorDownload'. Also, I have tried getting the entire path as follows - ...This works when the button is placed within some other container but does not work with gridview. – user71934 Jan 19 '18 at 12:12

1 Answers1

0

The problem is that those controls are in a template so you cannot reference them directly. Use the RowDataBound event and assign the buttons a trigger programatically.

protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
    //check if the row is a datarow and not the first row
    if (e.Row.RowType == DataControlRowType.DataRow)
    {
        //find the button with findcontrol
        LinkButton lb = e.Row.FindControl("lnkLogFiles") as LinkButton;

        //assign the button as a postback trigger
        ScriptManager.GetCurrent(Page).RegisterPostBackControl(lb);
    }
}
VDWWD
  • 35,079
  • 22
  • 62
  • 79