为什么我的按钮单击事件处理程序没有被调用?

发布于 2024-11-30 15:42:39 字数 1737 浏览 2 评论 0原文

我在 Page_PreInit 事件上动态创建了一个 RadGrid 控件,并将其添加到页面上的占位符中。我的 RadGridBuilder 类有一个 Build() 方法。

我的 RadGrid 有一个自定义按钮,该按钮是在生成 RadGrid (Page_PreInit) 期间调用的 ItemCreated 事件时添加的:

protected virtual void RdGridItemCreated(object sender, GridItemEventArgs e)
    {          
        switch (e.Item.ItemType)
        {            
            // other codes
            case GridItemType.CommandItem:
                {
                    var gridCommandItem = e.Item as GridCommandItem;

                    AddPdfButton(gridCommandItem);

                    break;
                }
        }
    }


private void AddPdfButton(GridCommandItem gridCommandItem)
    {
        var pdfButton = CreateExportToPdfButton();

        try
            {
                PageUtil.RegisterPostBackControl(pdfButton);

                // this is the cell which contains the export buttons.
                ((Table) gridCommandItem.Cells[0].Controls[0]).Rows[0].Cells[1].Controls.Add(pdfButton);

            }
            catch
            {
                // LOG the error silently
            }
        }
    }
 private Button CreateExportToPdfButton()
    {
        var result = new Button();
        result.ID = "btnExportToPdf";
        result.Click += ExportToPdfButtonClick;
        result.CssClass = "rgExpPDF";
        result.CommandName = "ExportToPdfCustomCommand";
        result.Attributes["title"] = "Export to Pdf";
        return result;
    }
private void ExportToPdfButtonClick(object sender, EventArgs e)
   {
        // custom code
   }

Pdf 图标按预期显示在 RadGrid 上。单击它时,会发生回发(并且 RadGrid 显然会在 Page_PreInit 上再次重新生成),但是,永远不会调用 ExportToPdfButtonClick 方法。

为什么不叫它?如何修复它?它可能与视图状态和控制状态有关?

谢谢

I have created a RadGrid control dynamically on Page_PreInit event and added it to a place holder on the page. my RadGridBuilder class has a Build() method.

My RadGrid has a custom button which is added at the time of ItemCreated event which is called during building the RadGrid (Page_PreInit):

protected virtual void RdGridItemCreated(object sender, GridItemEventArgs e)
    {          
        switch (e.Item.ItemType)
        {            
            // other codes
            case GridItemType.CommandItem:
                {
                    var gridCommandItem = e.Item as GridCommandItem;

                    AddPdfButton(gridCommandItem);

                    break;
                }
        }
    }


private void AddPdfButton(GridCommandItem gridCommandItem)
    {
        var pdfButton = CreateExportToPdfButton();

        try
            {
                PageUtil.RegisterPostBackControl(pdfButton);

                // this is the cell which contains the export buttons.
                ((Table) gridCommandItem.Cells[0].Controls[0]).Rows[0].Cells[1].Controls.Add(pdfButton);

            }
            catch
            {
                // LOG the error silently
            }
        }
    }
 private Button CreateExportToPdfButton()
    {
        var result = new Button();
        result.ID = "btnExportToPdf";
        result.Click += ExportToPdfButtonClick;
        result.CssClass = "rgExpPDF";
        result.CommandName = "ExportToPdfCustomCommand";
        result.Attributes["title"] = "Export to Pdf";
        return result;
    }
private void ExportToPdfButtonClick(object sender, EventArgs e)
   {
        // custom code
   }

The Pdf icon appears as expected on the RadGrid. When it is clicked, the post back happens (and the RadGrid is regenerated obviously on Page_PreInit again), however, the ExportToPdfButtonClick method is never called.

Why it is not called? how to fix it? it may be related to viewstate and control state?

Thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(2

夏日浅笑〃 2024-12-07 15:42:39

RadGrid 似乎明显不同,没有弄清楚为什么没有引发该事件,但找到了解决方案。

我创建了一个实现 ITemplate 接口的新类,并将其分配给 RadGrid.MasterTableView 对象的 CommandItemTemplate 属性。

然后在新类的 Instantiate() 方法中添加自定义控件,最后引发单击事件;基本上重建我的命令行:

http://www.telerik.com/help /aspnet-ajax/grid-commanditemtemplate.html

不确定这些控件在什么时候添加到 RadGrid,但必须在 RadGrid_ItemCreated 和 RadGrid_Load 事件之前和之后RadGrid_Init 事件。无论如何,现在已经解决了。

RadGrid seems to be different apparently and didn't figure out why the event wasn't raised but found a solution.

I created a new class implementing ITemplate interface and assigning it to the CommandItemTemplate property of my RadGrid.MasterTableView object.

Then adding my custom controls in the Instantiate() method of my new class and finally the click event is raised; basically rebuilding my Command Row:

http://www.telerik.com/help/aspnet-ajax/grid-commanditemtemplate.html

Not sure at what point these controls are added to the RadGrid but must be before the RadGrid_ItemCreated and RadGrid_Load events and after RadGrid_Init event. anyway, it's resolved now.

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