Radgrid无法使用NestedViewtemplate中的linkbutton下载文件

时间:2013-07-11 15:15:27

标签: telerik telerik-grid radgrid

我正在使用radgrid nestedviewttemplate在行展开时显示产品的详细信息。我的嵌套视图模板中也有一个链接按钮,单击时会下载文件。我用于下载的代码在radgrid外部工作但在包含时Nestedviewtemplate无法下载。这是我的代码。

 <telerik:RadGrid ID="loggedInUserOwnResourcesRadGrid"   AutoGenerateColumns="false">
 <MasterTableView AutoGenerateColumns="false">
 <Columns>
 <telerik:GridTemplateColumn   DataField=" Name">
 <HeaderTemplate><asp:LinkButton ID="LinkButtonForTitleOfGridViewColumn"  
  runat="server" CommandName="Sort" CommandArgument="Name">Resource Name</asp:LinkButton>
 </HeaderTemplate>
 <ItemTemplate>
 <asp:LinkButton ID="LinkButtonOfAParticularName"  runat="server" Text='<%# Eval(" Name")%>'></asp:LinkButton>
  </ItemTemplate>
 </telerik:GridTemplateColumn>
 </Columns>
 <NestedViewSettings >
 <ParentTableRelation>
 <telerik:GridRelationFields DetailKeyField="ID" MasterKeyField="ID"/>
 </ParentTableRelation>
 </NestedViewSettings>
 <NestedViewTemplate>
 <asp:Panel ID="NestedViewPanel"  runat="server">
 <div >
 <fieldset >
 <legend ><b>Detail Info on Resource :   <%#Eval("Name") %></b>
 </legend>

  Download File :

 <asp:LinkButton ID="filenamelinkbutton" Text='<%# Eval("Filename")%>' CommandArgument='<%# 
  Eval("PathToFile")%>'  OnCommand="filenamelinkbutton_Clicked" CommandName="filenamelinkbutton_Clicked"
  runat="server"  ToolTip="Click To Download" ForeColor="Blue"  ></asp:LinkButton>

 </fieldset>
  </div>
 </asp:Panel>
</NestedViewTemplate>
  </MasterTableView>
</telerik:RadGrid>

  protected void filenamelinkbutton_Clicked(object sender, CommandEventArgs e)
    {


        string downloadfilename = e.CommandArgument.ToString();

            try
            {
                FileInfo resourcefilepathusingfileinfo = new FileInfo("~/" + downloadfilename);
                string filename = resourcefilepathusingfileinfo.Name;
                Response.ContentType = "application/download";
                Response.AppendHeader("Content-Disposition", "attachment;filename=" + filename);

                Response.TransmitFile(Server.MapPath("~/" + "ClientBin/" + "Uploads/" + filename));



                HttpContext.Current.ApplicationInstance.CompleteRequest();
             //   Response.End();

            }
            catch (Exception ex)
            { 


            }
        }

1 个答案:

答案 0 :(得分:2)

请尝试使用以下代码段。

  1. 如果您的网格与Telerik Ajax管理器一起使用。
  2. JS

        <script type="text/javascript">
            function onRequestStart(sender, args) {
                if (args.get_eventTarget().indexOf("filenamelinkbutton") >= 0) {
                    args.set_enableAjax(false);
                }
            }
        </script>
    

    ASPX

    <telerik:RadAjaxManager ID="RadAjaxManager1" runat="server" ClientEvents-OnRequestStart="onRequestStart">
        <AjaxSettings>
            <telerik:AjaxSetting AjaxControlID="RadGrid1">
                <UpdatedControls>
                    <telerik:AjaxUpdatedControl ControlID="RadGrid1" />
                </UpdatedControls>
            </telerik:AjaxSetting>
        </AjaxSettings>
    </telerik:RadAjaxManager>
    
    1. 如果您的网格使用更新面板ajaxify。
    2. ASPX

      <triggers>         
           <asp:PostBackTrigger ControlID="filenamelinkbutton" />
      </triggers>
      

      OR

      C#

      protected void RadGrid1_ItemDataBound(object sender, GridItemEventArgs e)
      {
          if (e.Item is GridNestedViewItem)
          {
              GridNestedViewItem nestedItem = (GridNestedViewItem)e.Item;
              LinkButton filenamelinkbutton = (LinkButton)nestedItem.FindControl("filenamelinkbutton");
              ScriptManager.GetCurrent(Page).RegisterPostBackControl(filenamelinkbutton);
          }
      
      }