简体   繁体   English

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

[英]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.

相关问题 updatepanel.update()似乎没有从隐藏代码中触发 - updatepanel.update() doesn't appear to fire from code-behind 从代码隐藏(C#、WPF)添加时,用户控件无法在 ListBox 中正确显示 - UserControls aren't displayed properly in ListBox when added from Code-behind (C#, WPF) ItemCommand事件不会与Repeater控件一起触发 - ItemCommand event doesn't fire with the Repeater control 在 WPF 中的代码隐藏中更改时,int 类型的属性不会反映在 UI 中? - Property of type int doesn't reflect in UI when changed in code-behind in WPF? 背后的代码无法识别标签名称 - Code-behind doesn't recognize the name of label 回发后通过背后的代码添加到更新面板的项目不会触发事件 - Event doesn't fire for an item that is added to the update panel through code behind after post back ItemCommand事件不会与母版页中的中继器控件一起触发 - ItemCommand event doesn't fire with repeater control in master page asp.net找不到从div的代码隐藏添加的控件 - asp.net Can't find controls added from code-behind in a div 在后台创建代码的Checkboxlist不会在UpdatePanel中触发OnSelectedIndexChanged - Code-behind created Checkboxlist wont fire OnSelectedIndexChanged in UpdatePanel 在所有窗口中使用代码隐藏添加DynamicResource - Using code-behind added DynamicResource in all windows
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM