This is a migrated thread and some comments may be shown as answers.

Writing pdf file to browser on linkbutton click from inside detailview gridtableview

5 Answers 57 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Stephen
Top achievements
Rank 1
Stephen asked on 30 Apr 2014, 09:50 AM
I'm having trouble writing a pdf file to the browser when a user clicks a linkbutton inside a radgrid detailview gridtableview gridtemplatecolumn.  The grid is also wrapped in an update panel. The click event fires and the bytes are written to the response without error.  However the browser's file download dialog never appears.

Any insights would be greatly appreciated.

<asp:UpdatePanel ID="updatePanel" ClientIDMode="Static" runat="server">
            <ContentTemplate>
    <telerik:RadGrid ID="rgDocuments" runat="server" Width="100%" ShowStatusBar="False" AutoGenerateColumns="False"
        AllowSorting="False" AllowMultiRowSelection="False" AllowPaging="False" Skin="EACS" EnableEmbeddedSkins="false"
        OnDetailTableDataBind="rgDocuments_DetailTableDataBind" OnNeedDataSource="rgDocuments_NeedDataSource"
        OnPreRender="rgDocuments_PreRender">
        <MasterTableView Width="100%" DataKeyNames="DOCUMENT_AUTHOR_ID" AllowMultiColumnSorting="True">
            <DetailTables>
                <telerik:GridTableView DataKeyNames="DOCUMENT_AUTHOR_ID,DOCUMENT_AUTHOR_CATEGORY_ID" Name="Categories" Width="100%" >
                    <DetailTables>
                        <telerik:GridTableView DataKeyNames="DOCUMENT_ID,PARENT_DOCUMENT_ID" Name="ParentDocuments" Width="100%">
                            <DetailTables>
                                <telerik:GridTableView DataKeyNames="DOCUMENT_ID,FILENAME,TITLE" Name="Documents" Width="100%">                           
                                    <Columns>
                                        <telerik:GridBoundColumn DataField="DOCUMENT_ID" Visible="false">
                                        </telerik:GridBoundColumn>
                                        <telerik:GridBoundColumn DataField="FILENAME" Visible="false">
                                        </telerik:GridBoundColumn>
                                        <telerik:GridTemplateColumn HeaderText="Section Title">
                                            <ItemTemplate>
                                                   <asp:LinkButton ID="btnDocument" runat="server" OnClick="btnDocument_Click" Text='<%# Eval("Title") %>'></asp:LinkButton>
                                              </ItemTemplate>
                                        </telerik:GridTemplateColumn>
                                    </Columns>
                                </telerik:GridTableView>
                            </DetailTables>
                            <Columns>
                                <telerik:GridBoundColumn DataField="DOCUMENT_ID" Visible="false">
                                </telerik:GridBoundColumn>                               
                                <telerik:GridBoundColumn HeaderText="Document Title" DataField="TITLE" >
                                </telerik:GridBoundColumn>
                            </Columns>
                        </telerik:GridTableView>
                    </DetailTables>
                    <Columns>
                        <telerik:GridBoundColumn DataField="DOCUMENT_AUTHOR_CATEGORY_ID" Display="false">
                        </telerik:GridBoundColumn>
                        <telerik:GridBoundColumn HeaderText="Category" DataField="DISPLAY_VALUE">
                        </telerik:GridBoundColumn>
                    </Columns>
                </telerik:GridTableView>
            </DetailTables>
            <Columns>
                <telerik:GridBoundColumn DataField="DOCUMENT_AUTHOR_ID" Visible="false"></telerik:GridBoundColumn>
                <telerik:GridBoundColumn HeaderText="Author Name" DataField="DISPLAY_VALUE"></telerik:GridBoundColumn>
            </Columns>
        </MasterTableView>
    </telerik:RadGrid>
    </ContentTemplate>
    </asp:UpdatePanel>

protected void btnDocument_Click(object sender, EventArgs e)
        {
            LinkButton lb = (LinkButton)sender;
            GridDataItem item = (GridDataItem)lb.NamingContainer;
            if (item != null)
            {
                string documentUploadPath = ConfigurationManager.AppSettings["DocumentUploadPath"].ToString();               
                string filename = documentUploadPath + item.GetDataKeyValue("FILENAME").ToString();
                string title = documentUploadPath + item.GetDataKeyValue("TITLE").ToString();
 
                if (File.Exists(Server.MapPath(filename)))
                {
                    System.Web.HttpResponse response = System.Web.HttpContext.Current.Response;
 
                    byte[] bytes = File.ReadAllBytes(Server.MapPath(filename));
 
                    response.Clear();
                    response.AddHeader("Content-Type", "binary/octet-stream");
                    response.AddHeader("Content-Disposition",
                        "attachment; filename=" + title + ".pdf;");
                    response.Flush();
                    response.BinaryWrite(bytes);
                    response.Flush();
                    response.End();
                }
            }
        }

5 Answers, 1 is accepted

Sort by
0
Abdul Rahim Shaik
Top achievements
Rank 2
answered on 30 Apr 2014, 10:06 AM
Hi Stephen,

As the radgrid is inside the updatepanel, this will restricts the file download dailog.
Can you comment the updatepanel code and check it once.
0
Princy
Top achievements
Rank 1
answered on 30 Apr 2014, 10:13 AM
Hi Stephen,

Please note that the exporting feature of the control work with regular postbacks only and it could not function correctly with ajax request. In case you export from a Button that is nested in MS AJAX UpdatePanel, you should set this control as PostBackTrigger: Please refer to the help topic below which elaborates on this matter:
 http://www.telerik.com/help/aspnet-ajax/grid-export-with-ajax-enabled.html

Thanks,
Princy
0
Stephen
Top achievements
Rank 1
answered on 30 Apr 2014, 10:21 AM
Could you provide me with example code that demonstrates how to register the linkbutton (btnDocument) as a postback control within each data row?
0
Stephen
Top achievements
Rank 1
answered on 30 Apr 2014, 10:21 AM
Could you provide me with example code that demonstrates how to register the linkbutton (btnDocument) as a postback control within each data row?
0
Stephen
Top achievements
Rank 1
answered on 30 Apr 2014, 10:38 AM
Thanks for sending me down the right track Princy.  I was able to sort out the issue by using the radgrid itemdatabound event.

protected void rgDocuments_ItemDataBound(object sender, GridItemEventArgs e)
        {
            if (e.Item is GridDataItem && e.Item.OwnerTableView.Name == "Documents")
            {
                GridDataItem item = (GridDataItem)e.Item;
                LinkButton btn = (LinkButton)item.FindControl("btnDocument");
                ScriptManager.GetCurrent(Page).RegisterPostBackControl(btn);
            }
        }
Tags
Grid
Asked by
Stephen
Top achievements
Rank 1
Answers by
Abdul Rahim Shaik
Top achievements
Rank 2
Princy
Top achievements
Rank 1
Stephen
Top achievements
Rank 1
Share this question
or