在代码隐藏中添加时,RadGrid ItemCommand 不会触发
RadGrid ItemCommand Doesn't Fire When Added In Code-Behind
我在 ASP.NET 网站中使用 Telerik RadGrid,我需要从我的页面的 PreRender 事件中以编程方式添加 ItemCommand 事件处理程序:
MyGrid.ItemCommand += new GridCommandEventHandler(OnItemCommand);
但是,OnItemCommand
从未被调用过。如果我把它放在 .aspx 文件中它工作正常:OnItemCommand="OnItemCommand"
,只是当我在 C# 中添加它时不是。
这是怎么回事? TYIA.
创建网格时添加处理程序(在 Page_Load 或 Page_Init 中):http://www.telerik.com/help/aspnet-ajax/grid-programmatic-creation.html。或者,只是在标记中。如果您需要它仅在特定条件下触发,您可以在处理程序本身中添加该条件并退出它。
编辑:
尝试使用以下代码作为基础,因为它对我来说效果很好:
<telerik:RadGrid ID="RadGrid1" runat="server" OnNeedDataSource="RadGrid1_NeedDataSource">
<MasterTableView>
<Columns>
<telerik:GridEditCommandColumn></telerik:GridEditCommandColumn>
</Columns>
</MasterTableView>
</telerik:RadGrid>
和服务器端:
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();
}
我在 ASP.NET 网站中使用 Telerik RadGrid,我需要从我的页面的 PreRender 事件中以编程方式添加 ItemCommand 事件处理程序:
MyGrid.ItemCommand += new GridCommandEventHandler(OnItemCommand);
但是,OnItemCommand
从未被调用过。如果我把它放在 .aspx 文件中它工作正常:OnItemCommand="OnItemCommand"
,只是当我在 C# 中添加它时不是。
这是怎么回事? TYIA.
创建网格时添加处理程序(在 Page_Load 或 Page_Init 中):http://www.telerik.com/help/aspnet-ajax/grid-programmatic-creation.html。或者,只是在标记中。如果您需要它仅在特定条件下触发,您可以在处理程序本身中添加该条件并退出它。
编辑: 尝试使用以下代码作为基础,因为它对我来说效果很好:
<telerik:RadGrid ID="RadGrid1" runat="server" OnNeedDataSource="RadGrid1_NeedDataSource">
<MasterTableView>
<Columns>
<telerik:GridEditCommandColumn></telerik:GridEditCommandColumn>
</Columns>
</MasterTableView>
</telerik:RadGrid>
和服务器端:
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();
}