Telerik RadGrid ExportToPDF() 或 ExportToExcel() 不起作用
我有一个继承 RadGrid 的简单类。我正在向 RadGrid 添加一个按钮,并向该按钮添加一个 Click 事件处理程序。按钮已正确添加到所需位置,并且单击事件处理程序正在触发,但 radGrid.ExportToExcel() 没有执行任何操作。事实上,点击后以及页面回发时,该按钮就会消失。为什么会发生这种情况?
我尝试将按钮控件添加到 Page.Form
控件集合中,但仍然没有任何反应。
[ToolboxData("<{0}:RadGridDp runat=server></{0}:RadGridDp>")]
public class RadGridDP : RadGrid
{
public RadGridDP()
{
}
protected override void OnLoad(EventArgs e)
{
base.OnLoad(e);
Button btnExport = new Button();
btnExport.ID = "Export";
btnExport.Text = "Export";
btnExport.Click += new EventHandler(btnExport_Click);
btnExport.CommandArgument = this.ID;
this.MasterTableView.Controls.Add(btnExport);
}
void btnExport_Click(object sender, EventArgs e)
{
Button btnExport = (Button)sender;
string RadGridId = btnExport.CommandArgument.ToString();
RadGridDP radGrid = (RadGridDP)this.Parent.Parent.FindControl(RadGridId);
radGrid.ExportSettings.IgnorePaging = true;
radGrid.ExportSettings.OpenInNewWindow = true;
radGrid.ExportSettings.ExportOnlyData = true;
radGrid.MasterTableView.ExportToExcel();
}
}
当我在 UserControl
中执行相同的操作并在任何页面上使用该 UserControl
时,它工作正常。有什么区别?
I have a simple class inheriting RadGrid
. I am adding a button to the RadGrid and a Click Event handler to that button. The button is correctly added in the required position and the click event handler is firing, but radGrid.ExportToExcel()
is not doing anything. In fact, upon click and when page posts back, the button disappears. Why is this happening?
I tried to add the button control to the Page.Form
control collection, but still nothing happens.
[ToolboxData("<{0}:RadGridDp runat=server></{0}:RadGridDp>")]
public class RadGridDP : RadGrid
{
public RadGridDP()
{
}
protected override void OnLoad(EventArgs e)
{
base.OnLoad(e);
Button btnExport = new Button();
btnExport.ID = "Export";
btnExport.Text = "Export";
btnExport.Click += new EventHandler(btnExport_Click);
btnExport.CommandArgument = this.ID;
this.MasterTableView.Controls.Add(btnExport);
}
void btnExport_Click(object sender, EventArgs e)
{
Button btnExport = (Button)sender;
string RadGridId = btnExport.CommandArgument.ToString();
RadGridDP radGrid = (RadGridDP)this.Parent.Parent.FindControl(RadGridId);
radGrid.ExportSettings.IgnorePaging = true;
radGrid.ExportSettings.OpenInNewWindow = true;
radGrid.ExportSettings.ExportOnlyData = true;
radGrid.MasterTableView.ExportToExcel();
}
}
When I do same thing in a UserControl
and use that UserControl
on any page, it works fine. What's the difference?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我找到了解决办法。每当 RadGrid 加载时,它都会以这种方式调用各种事件:
点击按钮(按上面的方式添加),就会以这种方式调用事件
(对于奇怪的序列号,这些事件之间可能还有其他事件,但是出现顺序正确)
因此,整个网格都随数据反弹,因此导出 PDF 的命令被刷新。所以什么也没有发生。
有趣的是,不需要添加一个额外的按钮,Telerik 提供了自己的按钮来执行此操作。也可以自定义(通过实现 ITemplate)。这就是我现在生成报告的方式(尽管不是特定于原始问题):
所以基本上我必须处理 PdfExporting(对于 Pdf)和 GridExporting(对于 excel)..
我还必须处理 Load、ItemCommand 和 ItemCreated。虽然前一个是某些条件逻辑所需要的,但后两个是 PDF 文档格式所需要的
I found out the solution. Whenever RadGrid Loads, it calls various events in this fashion:
and upon click of the button (added in the manner above), events are called in this fashion
(as for strange serial numbers, these events may have other events in between, but the order of occurance is correct)
so, entire Grid is rebound with the data, and hence command to exportPdf is flushed. So nothing happens.
Interestingly, there's no need of adding one extra button, telerik provides its own buttons to do so. which can be customized as well(by implementing ITemplate). This is how am generating Reports now(though not specific to the original question):
so basically i had to handle PdfExporting(for Pdf) and GridExporting(for excel)..
I had to handle Load, ItemCommand and ItemCreated as well. While the former one was required for some conditional logic, later two were required for formatting of the PDF document