[英]RadGrid ItemCommand Doesn't Fire When Added In Code-Behind
I am using a Telerik RadGrid in an ASP.NET website and I need to programmatically add an ItemCommand event handler from my page's PreRender event: 我在ASP.NET网站中使用Telerik RadGrid,并且需要以编程方式从页面的PreRender事件添加ItemCommand事件处理程序:
MyGrid.ItemCommand += new GridCommandEventHandler(OnItemCommand);
However, OnItemCommand
is never called. 但是,永远不会调用
OnItemCommand
。 If I put it in the .aspx file it works fine: OnItemCommand="OnItemCommand"
, just not when I add it in C#. 如果我将其放在.aspx文件中,则可以正常工作:
OnItemCommand="OnItemCommand"
,但是当我在C#中添加它时却不能。
What's going on here? 这里发生了什么? TYIA.
TYIA。
Add the handler when you create the grid (in Page_Load or Page_Init): http://www.telerik.com/help/aspnet-ajax/grid-programmatic-creation.html . 创建网格时(在Page_Load或Page_Init中)添加处理程序: http : //www.telerik.com/help/aspnet-ajax/grid-programmatic-creation.html 。 Or, simply in the markup.
或者,仅在标记中。 If you need it to fire only on a certain condition, you can add that condition inside the handler itself and exit it.
如果您只需要在特定条件下触发它,则可以将该条件添加到处理程序本身中并退出。
EDIT: Try using the following code as base as it worked fine for me: 编辑:尝试使用以下代码作为基础,因为它对我来说效果很好:
<telerik:RadGrid ID="RadGrid1" runat="server" OnNeedDataSource="RadGrid1_NeedDataSource">
<MasterTableView>
<Columns>
<telerik:GridEditCommandColumn></telerik:GridEditCommandColumn>
</Columns>
</MasterTableView>
</telerik:RadGrid>
and server side: 和服务器端:
protected void Page_Load(object sender, EventArgs e)
{
RadGrid1.ItemCommand += RadGrid1_ItemCommand;
}
protected void RadGrid1_ItemCommand(object sender, GridCommandEventArgs e)
{
throw new NotImplementedException();
}
protected DataTable GetData()
{
DataTable tbl = new DataTable();
tbl.Columns.Add(new DataColumn("Description"));
tbl.Columns.Add(new DataColumn("ParameterName"));
tbl.Columns.Add(new DataColumn("ThirdColumn"));
tbl.Columns.Add(new DataColumn("FourthColumn"));
tbl.Rows.Add(new object[] { "firstRecord1", "firstRecord2", "firstRecord3", "firstRecord4" });
tbl.Rows.Add(new object[] { "secondRecord1", "secondRecord2", "secondRecord3", "secondRecord4" });
tbl.Rows.Add(new object[] { "thirdRecord1", "thirdRecord2", "thirdRecord3", "thirdRecord4" });
tbl.Rows.Add(new object[] { "fourthRecord1", "fourthRecord2", "fourthRecord3", "fourthRecord4" });
return tbl;
}
protected void RadGrid1_NeedDataSource(object sender, GridNeedDataSourceEventArgs e)
{
RadGrid1.DataSource=GetData();
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.