RadGrid ItemCommand在代码隐藏中添加时不会触发

时间:2015-03-03 21:26:24

标签: c# asp.net telerik radgrid

我在ASP.NET网站中使用Telerik RadGrid,我需要以编程方式从我的页面的PreRender事件中添加ItemCommand事件处理程序:

MyGrid.ItemCommand += new GridCommandEventHandler(OnItemCommand);

但是,永远不会调用OnItemCommand。如果我把它放在.aspx文件中它可以正常工作:OnItemCommand="OnItemCommand",当我在C#中添加它时就不行了。

这里发生了什么? TYIA。

1 个答案:

答案 0 :(得分:1)

在创建网格时添加处理程序(在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();
}