gpt4 book ai didi

javascript - 显示有关导出完整 RadGrid Telerik Web 组件的数据的 javascript 警报

转载 作者:行者123 更新时间:2023-12-03 03:51:01 26 4
gpt4 key购买 nike

我正在使用名为 RadGrid 的 Telerik Web 组件,该组件与 Asp.Net ObjectDataSource 绑定(bind)。它允许将绑定(bind)数据导出为 Excel/PDF/Word 格式。问题是文件下载完成后我无法触发 JavaScript 警报。我已经在 J​​avascript 上尝试过 OnResponseEnd 方法,但它不起作用。

有什么建议吗?

这是迄今为止我的代码隐藏代码。

protected void bXls_Click(object sender, EventArgs e)
{
RadGrid1.MasterTableView.GetColumn("Historico").Visible = false;
RadGrid1.MasterTableView.GetColumn("TareaIdExport").Visible = true;

RadGrid1.ExportSettings.IgnorePaging = true;
RadGrid1.ExportSettings.OpenInNewWindow = false;
RadGrid1.MasterTableView.ExportToExcel();
}

以及组件代码的简短版本

<telerik:RadGrid ID="RadGrid1" runat="server"
AutoGenerateColumns="False"
Culture="es-ES"
GroupPanelPosition="Top" DataSourceID="objGrid"
OnItemCommand="RadGrid1_ItemCommand"
OnItemDataBound="RadGrid1_ItemDataBound"
RenderMode="Lightweight"
OnPreRender="RadGrid1_PreRender1"
AllowFilteringByColumn="True"
AllowPaging="True"
AllowSorting="True"
OnItemCreated="RadGrid1_ItemCreated"
PageSize="4"
OnGridExporting="RadGrid1_GridExporting"
OnPdfExporting="RadGrid1_PdfExporting">
</telerik:RadGrid>

最佳答案

由于导出需要完整的回发,因此不能使用 ajax 来限制从帖子返回的内容。这使得处理整个事件变得困难。您可以做的是使用将从导出的完整回发中返回的 cookie。以下教程使用此方法来解决导出期间显示 AjaxLoadingPanel 的类似问题。

解决方案总结如下。导出完成后,服务器端的 cookie 将添加到表单中。同时,客户端正在轮询并寻找此 cookie。当客户端找到cookie时,表示导出完成。

Show loading panel when exporting RadGrid

以下是根据您的场景调整的代码工作示例:

ASPX:

function gridCommand(sender, args) {
if (args.get_commandName().startsWith("Export")) {

//initiate cookie polling
appendDownloadToken();
}
}

function appendDownloadToken() {
window._downloadToken = new Date().getTime() + "";
//add a form field containing the download token before submit
$telerik.$("<input type='hidden' id='_downloadToken' name='_downloadToken' value='" + window._downloadToken + "' />").appendTo(document.forms[0]);

pollDownloadCookie();
}


function pollDownloadCookie() {
//compare cookie value and initial value
if (cookie.get("_downloadToken") === window._downloadToken) {
//erase download token cookie
cookie.erase("_downloadToken");
//remove the token value
delete window._downloadToken;
//remove the form field
$telerik.$("#_downloadToken").remove();
//show alert
alert('Grid Exported');
} else {
setTimeout(pollDownloadCookie, 100);
}
}

//Helper method to deal with cookies
cookie = {
get: function(name) {
var part = document.cookie.split(name + "=")[1];
return part ? decodeURIComponent(part.split(";")[0]) : null;
},
set: function(name, value, days, path, secure) {
document.cookie = [
name + "=" + encodeURIComponent(value),
days ? "expires=" + new Date(new Date().getTime() + (days * 24 * 60 * 60 * 1000)).toUTCString() : "",
secure ? "secure" : "",
path ? "path=" + path : "path=/"
].join("; ");
},
erase: function(name) {
cookie.set(name, "", -1);
},
all: function() {
var ret = {};
var arr = document.cookie.split(";");
for (var i = 0; i < arr.length; i++) {
if (arr[i]) {
var pair = arr[i].split("=");
ret[pair[0]] = decodeURIComponent(pair[1]);
}
}
return ret;
}
}
<telerik:RadGrid ID="RadGrid1" runat="server" OnNeedDataSource="RadGrid1_NeedDataSource" AutoGenerateColumns="false" OnItemCommand="RadGrid1_ItemCommand">
<ExportSettings ExportOnlyData="True" HideStructureColumns="True" OpenInNewWindow="True"></ExportSettings>
<ClientSettings>
<ClientEvents OnCommand="gridCommand" />
</ClientSettings>
<MasterTableView DataKeyNames="ID" CommandItemDisplay="Top">
<CommandItemSettings ShowExportToExcelButton="true" ShowExportToCsvButton="true" ShowExportToPdfButton="true" ShowExportToWordButton="true" />
<Columns>
<telerik:GridBoundColumn UniqueName="Id" DataField="ID" HeaderText="ID"></telerik:GridBoundColumn>
<telerik:GridBoundColumn UniqueName="Name" DataField="Name" HeaderText="Name"></telerik:GridBoundColumn>
</Columns>
</MasterTableView>
</telerik:RadGrid>

C#:

 protected void RadGrid1_NeedDataSource(object sender, 
GridNeedDataSourceEventArgs e)
{
DataTable table = new DataTable();
table.Columns.Add("ID", typeof(int));
table.Columns.Add("Name");
for (int i = 1; i <= 5; i++) {
table.Rows.Add(i, "Name" + i.ToString());
}

RadGrid1.DataSource = table;

}

protected void RadGrid1_ItemCommand(object sender, GridCommandEventArgs e)
{
if (e.CommandName.StartsWith("Export")) {
if (!String.IsNullOrEmpty(Request("_downloadToken"))) {
Response.Cookies.Add(new HttpCookie("_downloadToken",
Request("_downloadToken")));
}
}
}

关于javascript - 显示有关导出完整 RadGrid Telerik Web 组件的数据的 javascript 警报,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45153342/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com