对导出完整 RadGrid Telerik Web 组件的数据显示 javascript 警报
Show javascript alert on data exporting complete RadGrid Telerik web component
我正在使用名为 RadGrid 的 Telerik Web 组件,该组件与 Asp.Net ObjectDataSource 绑定。它允许将绑定的数据导出为 Excel/PDF/Word 格式。
问题是我无法在文件下载完成后触发 javascript 警报。
我已经在 Javascript 上尝试了 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>
因为导出需要完整的 post 返回,所以 ajax 不能用于限制从 post 返回的内容。这使得处理整个事件变得困难。您可以做的是使用一个 cookie,该 cookie 将从完整的 post 导出返回。以下教程使用此方法解决在导出期间显示 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")));
}
}
}
我正在使用名为 RadGrid 的 Telerik Web 组件,该组件与 Asp.Net ObjectDataSource 绑定。它允许将绑定的数据导出为 Excel/PDF/Word 格式。 问题是我无法在文件下载完成后触发 javascript 警报。 我已经在 Javascript 上尝试了 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>
因为导出需要完整的 post 返回,所以 ajax 不能用于限制从 post 返回的内容。这使得处理整个事件变得困难。您可以做的是使用一个 cookie,该 cookie 将从完整的 post 导出返回。以下教程使用此方法解决在导出期间显示 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")));
}
}
}