Accessing RadComboBox in codebehind from a RadGrid

2019-09-02 19:27发布

I´m having trouble grabbing RadComboBox when editing inline from a RadGrid. When I try to find the correct control through this GridEditableItem I always receive null. Can anyone throw me some pointers in how to access the RadCombobox from my code below?

My ascx.cs file:

<telerik:RadGrid runat="server" ID="grid_AccessRecords"
    AllowPaging="True"
    AllowSorting="True"
    Visible="False" 
    Width="100%"
    PageSize="25" 
    OnItemCommand="AccessRecordsGridOnItemCommand"
    OnNeedDataSource="AccessRecordGridNeedDataSource">
    <PagerStyle Position="TopAndBottom" />
    <ClientSettings EnableRowHoverStyle="true" />  
    <MasterTableView DataKeyNames="Id" AutoGenerateColumns="False" EditMode="EditForms">
        <Columns>
            <telerik:GridTemplateColumn HeaderText="Eign" UniqueName="tmp_AccessGroup">
                <ItemTemplate>
                    <asp:label runat="server" ID="lbl_accessGroupName" Text='<%# Eval("AccessGroupName") %>' />
                </ItemTemplate>
                <EditItemTemplate>
                    <telerik:RadComboBox runat="server" ID="combo_editAccessGroup"></telerik:RadComboBox>
                </EditItemTemplate>
            </telerik:GridTemplateColumn>
            <telerik:GridEditCommandColumn UniqueName="EditCommandColumn" EditText="edit" ButtonType="ImageButton" EditImageUrl="/_layouts/images/AFLSharepoint2010/Edit.gif" />
            <telerik:GridButtonColumn CommandName="Delete" Text="delete" ConfirmText="Are you sure?" ConfirmDialogType="RadWindow" ConfirmTitle="Delete"
                ButtonType="ImageButton" UniqueName="DeleteColumn" ImageUrl="/_layouts/images/AFLSharepoint2010/Delete.gif" />
        </Columns>
        <EditFormSettings ColumnNumber="1" CaptionDataField="Id" CaptionFormatString="derp">
        EditColumn ButtonType="ImageButton" InsertText="Save" UpdateText="Save" UniqueName="EditCommandColumn" CancelText="Cancel" />
        </EditFormSettings>
    </MasterTableView>
</telerik:RadGrid>

The my cs file:

    protected void AccessRecordsGridOnItemCommand(object sender, GridCommandEventArgs e)
    {
        GridEditableItem editableItem = e.Item as GridEditableItem;

        if (editableItem != null)
        {
            RadComboBox comboEditAccessGroup = (RadComboBox) editableItem.FindControl("combo_editAccessGroup");
            //TODO: find out why always null???
        }
    }

1条回答
够拽才男人
2楼-- · 2019-09-02 19:37

You should be able to access your combo box if you use the OnItemCreated method instead:

protected void AccessRecordsGrid_OnItemCreated(object sender, Telerik.Web.UI.GridItemEventArgs e)
{

    if (e.Item is GridEditableItem && e.Item.IsInEditMode)
    {
        //the item is in edit mode    
        GridEditableItem editedItem = e.Item as GridEditableItem;

        RadComboBox comboEditAccessGroup = (RadComboBox)editedItem.FindControl("combo_editAccessGroup");

    }
}
查看更多
登录 后发表回答