我有一个棘手的问题,希望我能解释清楚。
以下是我的问题:我有一个用于添加/编辑行的radgrid FormTemplate ,其中我使用了RadAsyncUpload控件来上传文件。在插入和更新时成功上载文件。另外,我在我的radgrid视图中配置了一个链接按钮,可以在需要时下载文件。
问题现在在这里:在上传文件后立即尝试从radgrid视图下载时,我无法下载它。相反,我需要先重新加载页面,然后我可以下载该文件。可能是什么问题呢?我还使用了 grid.rebind()方法,但这并不起作用。以下是我的代码的简短区域:
<Columns>
<telerik:GridButtonColumn Text="Download" HeaderText="Responsible" ItemStyle-ForeColor="Blue" CommandName="DownloadResponsibleAttachment" ColumnGroupName="Attachments"
ConfirmTextFields="ResponsibleAttachment" ConfirmTextFormatString="Would you like to download attachment?" HeaderStyle-Width="30px"
ConfirmDialogType="RadWindow" Reorderable="false" UniqueName="DownloadResponsibleAttachment" ConfirmTitle="File Download">
</telerik:GridButtonColumn>
</Columns>
<EditFormSettings EditFormType="Template">
<FormTemplate>
<p class="lbl" style="font: 10px verdana, arial; color: #166c92; font-weight: bold;">Responsible Attachment</p>
<telerik:RadAsyncUpload ID="attResponsible" runat="server" InitialFileInputsCount="1" MaxFileInputsCount="1" MaxFileSize="1048576"
TargetFolder="~/Attachments" TemporaryFolder="~/Attachments" TemporaryFileExpiration="600000" ControlObjectsVisibility="None"
OverwriteExistingFiles="true" ToolTip="You can only upload one file with maximum 1 MB." OnFileUploaded="attResponsible_FileUploaded" >
</telerik:RadAsyncUpload>
<div style="float:right; padding-top:20px;">
<asp:Button ID="btnUpdate" CssClass="btn" Text='<%# (Container is GridEditFormInsertItem) ? "Insert" : "Update" %>'
runat="server" CommandName='<%# (Container is GridEditFormInsertItem) ? "PerformInsert" : "Update" %>'></asp:Button>
<asp:Button ID="btnCancel" CssClass="btn" Text="Cancel" runat="server" CausesValidation="False"
CommandName="Cancel"></asp:Button>
</FormTemplate>
</EditFormSettings>
答案 0 :(得分:0)
如果您要为AJAX-enabled control
(位于RadAjaxPanel or ajaxified by RadAjaxManager
中)的文件调用下载,则无法使用普通response.WriteFile function
。
Response.WriteFile("~/filename");
因为XmlHttpRequest
无法处理此问题。
修改强>
Please check this link for alternative
ASPX代码:
<div>
<telerik:RadScriptManager ID="RadScriptManager1" runat="server">
</telerik:RadScriptManager>
<telerik:RadWindowManager ID="RadWindowManager1" runat="server">
</telerik:RadWindowManager>
<asp:UpdatePanel ID="UpdatePanel1" runat="server" UpdateMode="Always">
<ContentTemplate>
<telerik:RadGrid ID="RadGrid1" runat="server" AutoGenerateColumns="false" OnItemCommand="RadGrid1_ItemCommand"
OnNeedDataSource="RadGrid1_NeedDataSource" OnItemDataBound="RadGrid1_ItemDataBound">
<MasterTableView Width="950" AutoGenerateColumns="false" DataKeyNames="ID" GridLines="None"
TableLayout="Auto">
<Columns>
<telerik:GridBoundColumn DataField="ID" HeaderText="Emp ID" ReadOnly="true" HeaderStyle-HorizontalAlign="Left"
ItemStyle-HorizontalAlign="Left" UniqueName="ID" FilterControlWidth="30px" AutoPostBackOnFilter="true"
CurrentFilterFunction="Contains" />
<telerik:GridButtonColumn DataTextField="Name" ItemStyle-ForeColor="Blue" CommandName="Generate"
ConfirmTextFields="Name" ConfirmTextFormatString="Would you like to {0} ACH file ?"
ConfirmDialogType="RadWindow" Reorderable="false" UniqueName="ButtonName" ConfirmTitle="ACH File">
</telerik:GridButtonColumn>
</Columns>
</MasterTableView>
</telerik:RadGrid>
</ContentTemplate>
</asp:UpdatePanel>
</div>
代码背后:
protected void RadGrid1_NeedDataSource(object sender, GridNeedDataSourceEventArgs e)
{
dynamic data = new[] {
new { ID = 1, Name ="Name_1"},
new { ID = 2, Name = "Name_2"},
new { ID = 3, Name = "Name_3"},
new { ID = 4, Name = "Name_4"},
new { ID = 5, Name = "Name_5"}
};
RadGrid1.DataSource = data;
}
protected void RadGrid1_ItemDataBound(object sender, GridItemEventArgs e)
{
if (e.Item is GridDataItem)
{
GridDataItem item = e.Item as GridDataItem;
ScriptManager.GetCurrent(Page).RegisterPostBackControl(item["ButtonName"].Controls[0]);
}
}
protected void RadGrid1_ItemCommand(object sender, Telerik.Web.UI.GridCommandEventArgs e)
{
if (e.CommandName == "Generate")
{
Response.ContentType = "text/plain";
Response.AppendHeader("Content-Disposition", "attachment;filename= errorLog.txt");
Response.AddHeader("content-length", "0");
Response.Flush();
Response.End();
}
}
答案 1 :(得分:0)
我已经应用了一个解决方案,可以在成功插入/更新记录时重新加载页面,如下所示:
protected void grid_ItemInserted(object source, GridInsertedEventArgs e)
{
if (e.Exception == null)
{
if (Session["FileUploaded"] != null)
Response.Redirect(Request.RawUrl);
}
}
答案 2 :(得分:0)
出于记录的目的,我想针对我遇到的问题发布这个答案。我找到了解决这个问题的方法。似乎 RadAjaxManager 存在问题所以我将其替换为 RadAjaxPanel ,这对我来说非常适合。