为什么我的按钮单击事件处理程序没有被调用?
我在 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
尝试按照
ASP.Net:为什么我的按钮的单击/命令事件没有在转发器中绑定/触发?
http://forums.asp.net/t/1129248.aspx/1/10< /a>
Try binding this event on page_init or page_load as per
ASP.Net: why is my button's click/command events not binding/firing in a repeater?
http://forums.asp.net/t/1129248.aspx/1/10
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.