Telerik RadGrid ExportToPDF() 或 ExportToExcel() 不起作用

发布于 2024-12-26 13:34:07 字数 1355 浏览 1 评论 0原文

我有一个继承 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

十年不长 2025-01-02 13:34:07

我找到了解决办法。每当 RadGrid 加载时,它都会以这种方式调用各种事件:

1. Page OnLoad
m. RadGrid OnLoad
x. NeedDataSource

点击按钮(按上面的方式添加),就会以这种方式调用事件

1. Page_OnLoad
m. RadGrid OnLoad
n. btnExport_Click
x. NeedDataSource

(对于奇怪的序列号,这些事件之间可能还有其他事件,但是出现顺序正确)
因此,整个网格都随数据反弹,因此导出 PDF 的命令被刷新。所以什么也没有发生。
有趣的是,不需要添加一个额外的按钮,Telerik 提供了自己的按钮来执行此操作。也可以自定义(通过实现 ITemplate)。这就是我现在生成报告的方式(尽管不是特定于原始问题):

    [ToolboxData("<{0}:RadGridDP runat=server></{0}:RadGridDP>")]
    public class RadGridDP : RadGrid
    {
        //custom logic
        public RadGridDP()
        {
            this.ItemCreated += new GridItemEventHandler(RadGrid_ItemCreated);
            this.Load += new EventHandler(RadGridDP_Load);
            this.ItemCommand += new GridCommandEventHandler(RadGrid_ItemCommand);
            this.PdfExporting += new OnGridPdfExportingEventHandler(RadGridDP_PdfExporting);
            this.GridExporting += new OnGridExportingEventHandler(RadGridDP_GridExporting);

            this.ExportSettings.ExportOnlyData = true;
            this.ExportSettings.IgnorePaging = true;
            // this.ExportSettings.OpenInNewWindow = true;

            DoPdfFormatting();
            DoExcelFormatting();

        }
        protected void RadGridDP_PdfExporting(object sender, GridPdfExportingArgs e)
        {
            e.RawHTML = e.RawHTML.Replace("border=\"1\"", "").Replace("style=\"", "style=\" border:0.5px Solid black; ")
                        .Replace("<thead>", String.Format("<thead>{0}", TableHeader)).Replace("</tbody>", String.Format("{0}</tbody>", TableFooter));
        }
        protected void RadGridDP_GridExporting(object sender, GridExportingArgs e)
        {
            e.ExportOutput = e.ExportOutput.Replace("<thead>", String.Format("<thead>{0}", TableHeader))
                             .Replace("</tbody>", String.Format("{0}</tbody>", TableFooter));
        }

  }

所以基本上我必须处理 PdfExporting(对于 Pdf)和 GridExporting(对于 excel)..
我还必须处理 Load、ItemCommand 和 ItemCreated。虽然前一个是某些条件逻辑所需要的,但后两个是 PDF 文档格式所需要的

I found out the solution. Whenever RadGrid Loads, it calls various events in this fashion:

1. Page OnLoad
m. RadGrid OnLoad
x. NeedDataSource

and upon click of the button (added in the manner above), events are called in this fashion

1. Page_OnLoad
m. RadGrid OnLoad
n. btnExport_Click
x. NeedDataSource

(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):

    [ToolboxData("<{0}:RadGridDP runat=server></{0}:RadGridDP>")]
    public class RadGridDP : RadGrid
    {
        //custom logic
        public RadGridDP()
        {
            this.ItemCreated += new GridItemEventHandler(RadGrid_ItemCreated);
            this.Load += new EventHandler(RadGridDP_Load);
            this.ItemCommand += new GridCommandEventHandler(RadGrid_ItemCommand);
            this.PdfExporting += new OnGridPdfExportingEventHandler(RadGridDP_PdfExporting);
            this.GridExporting += new OnGridExportingEventHandler(RadGridDP_GridExporting);

            this.ExportSettings.ExportOnlyData = true;
            this.ExportSettings.IgnorePaging = true;
            // this.ExportSettings.OpenInNewWindow = true;

            DoPdfFormatting();
            DoExcelFormatting();

        }
        protected void RadGridDP_PdfExporting(object sender, GridPdfExportingArgs e)
        {
            e.RawHTML = e.RawHTML.Replace("border=\"1\"", "").Replace("style=\"", "style=\" border:0.5px Solid black; ")
                        .Replace("<thead>", String.Format("<thead>{0}", TableHeader)).Replace("</tbody>", String.Format("{0}</tbody>", TableFooter));
        }
        protected void RadGridDP_GridExporting(object sender, GridExportingArgs e)
        {
            e.ExportOutput = e.ExportOutput.Replace("<thead>", String.Format("<thead>{0}", TableHeader))
                             .Replace("</tbody>", String.Format("{0}</tbody>", TableFooter));
        }

  }

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文