Radgrid 无法使用 NestedViewtemplate 中的链接按钮下载文件 [英] Radgrid unable to download file using linkbutton in NestedViewtemplate
问题描述
我正在使用 radgrid nestedviewttemplate 在行展开上显示产品的详细信息.我的 Nestedview 模板中还有一个链接按钮,单击时会下载文件.我用于下载的代码在 radgrid 之外工作,但当包含在Nestedviewtemplate 无法下载.这是我的代码.
<MasterTableView AutoGenerateColumns="false"><列><telerik:GridTemplateColumn DataField="名称"><HeaderTemplate><asp:LinkButton ID="LinkButtonForTitleOfGridViewColumn"runat="server" CommandName="Sort" CommandArgument="Name">资源名称</HeaderTemplate><项目模板><asp:LinkButton ID="LinkButtonOfAParticularName" runat="server" Text='<%# Eval(" Name")%>'></asp:LinkButton></ItemTemplate></telerik:GridTemplateColumn></列><NestedViewSettings ><父表关系><telerik:GridRelationFields DetailKeyField="ID" MasterKeyField="ID"/></ParentTableRelation></NestedViewSettings><NestedViewTemplate><asp:Panel ID="NestedViewPanel" runat="server"><div ><字段集><legend ><b>资源的详细信息:<%#Eval("Name") %></b></图例>下载文件 :<asp:LinkButton ID="filenamelinkbutton" Text='<%# Eval("Filename")%>'CommandArgument='<%#Eval("PathToFile")%>'OnCommand="filenamelinkbutton_Clicked" CommandName="filenamelinkbutton_Clicked"runat="server" ToolTip="点击下载" ForeColor="Blue" ></asp:LinkButton></fieldset>
</asp:面板></NestedViewTemplate></MasterTableView></telerik:RadGrid>protected void filenamelinkbutton_Clicked(object sender, CommandEventArgs e){字符串下载文件名 = e.CommandArgument.ToString();尝试{FileInfo resourcefilepathusingfileinfo = new FileInfo("~/" + downloadfilename);string filename = resourcefilepathusingfileinfo.Name;Response.ContentType = "申请/下载";Response.AppendHeader("Content-Disposition", "attachment;filename=" + filename);Response.TransmitFile(Server.MapPath("~/" + "ClientBin/" + "Uploads/" + filename));HttpContext.Current.ApplicationInstance.CompleteRequest();//Response.End();}捕获(异常前){}}
请尝试使用以下代码片段.
- 如果您的网格使用 Telerik Ajax 管理器进行 ajaxify.
JS
ASPX
<Ajax设置><telerik:AjaxSetting AjaxControlID="RadGrid1"><更新的控件><telerik:AjaxUpdatedControl ControlID="RadGrid1"/></UpdatedControls></telerik:AjaxSetting></AjaxSettings></telerik:RadAjaxManager>
- 如果您的网格使用更新面板进行 ajaxify.
ASPX
<触发器><asp:PostBackTrigger ControlID="filenamelinkbutton"/></触发器>
或
C#
protected void RadGrid1_ItemDataBound(object sender, GridItemEventArgs e){if (e.Item 是 GridNestedViewItem){GridNestedViewItemnestedItem = (GridNestedViewItem)e.Item;LinkButton 文件名linkbutton = (LinkButton)nestedItem.FindControl("filenamelinkbutton");ScriptManager.GetCurrent(Page).RegisterPostBackControl(filenamelinkbutton);}}
I'm using radgrid nestedviewttemplate to show the details of a product on row expand.I also have a linkbutton in my nestedview template which when clicked downloads the file.My code that i use for download works outside radgrid but when included in Nestedviewtemplate it fails to download.here's my code.
<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)
{
}
}
Please try with the below code snippet.
- If your grid ajaxify with Telerik Ajax manager.
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>
- If your grid ajaxify with update panel.
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);
}
}
这篇关于Radgrid 无法使用 NestedViewtemplate 中的链接按钮下载文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!