This is a migrated thread and some comments may be shown as answers.

RadGrid.FindControl does not find my DropDownList

4 Answers 98 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Danilo
Top achievements
Rank 1
Danilo asked on 10 Jul 2015, 10:59 AM

I'm trying to find a DropDownList in my RadGrid using FindControl() method. But it's always returning null so it cannot find the DropDownList. I have the following HTML code:

<telerik:RadGrid ID="RadGrid1" runat="server" AllowFilteringByColumn="True" DataSourceID="SqlDataSource1" GroupPanelPosition="Top" OnItemDataBound="RadGrid1_ItemDataBound">
    <ClientSettings>
        <Scrolling AllowScroll="True" UseStaticHeaders="True" />
    </ClientSettings>
    <MasterTableView AutoGenerateColumns="False" DataSourceID="SqlDataSource1" CommandItemDisplay="Top">
        <Columns>
            <telerik:GridTemplateColumn UniqueName="myName" HeaderText="header text">
                <EditItemTemplate>
                    <asp:DropDownList ID="myDropDown" DataTextField="TextFieldValue" DataValueField="ValueFieldValue" runat="server"/>
                </EditItemTemplate>
        </Columns>
    </MasterTableView>
</telerik:RadGrid>

 

And I'm trying to find "myDropDown" using this C# code:

protected void RadGrid1_ItemDataBound(object sender, GridItemEventArgs e)
{
    if (e.Item is GridDataItem)
    {
        GridDataItem item = (GridDataItem)e.Item;
        DropDownList list = (DropDownList)item.FindControl("myDropDown");
    }
}

 

But it cannot find the DropDownList. Can you tell me what I'm doing wrong?

Any help appreciated.

Regards,
Danilo

4 Answers, 1 is accepted

Sort by
0
Maria Ilieva
Telerik team
answered on 13 Jul 2015, 08:26 AM
Hi,

Try the following:
protected void RadGrid1_ItemDataBound(object sender, GridItemEventArgs e)
{
 if (e.Item is GridEditableItem && e.Item.IsInEditMode)
 {
        GridEditableItem item = (GridEditableItem)e.Item;
        DropDownList list = (DropDownList)item.FindControl("myDropDown");
 }
}


Regards,
Maria Ilieva
Telerik
Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Feedback Portal and vote to affect the priority of the items
0
Danilo
Top achievements
Rank 1
answered on 13 Jul 2015, 09:18 AM

Hi Maria

Thank you for your answer. But your code doesn't work as well. e.Item.IsInEditMode is always false. And even if I remove e.Item.IsInEditMode, the DropDownList cannot be found by the code.

Any other ideas?

0
Accepted
Pavlina
Telerik team
answered on 16 Jul 2015, 04:33 PM
Hi,

The code my colleague Maria sent you in the previous post shows how you can access the DropDownList control generated in the editable item, because the control is placed in EditItemTemplate. Note that you will need to use different approach for accessing the control depending to the chosen EditFormType. Could you please specify what is the EditMode in your case? If you don't need control to be in edit mode you should add the DropDownList in TemplateColumn ItemTemplate as shown below:

<telerik:GridTemplateColumn UniqueName="myName" HeaderText="header text">
                               <ItemTemplate>
                                   <asp:DropDownList ID="myDropDown" DataTextField="TextFieldValue" DataValueField="ValueFieldValue" runat="server" />
                               </ItemTemplate>
                           </telerik:GridTemplateColumn>

Additionally, for more information about accessing controls in Template column I recommend you examine the article below:
http://docs.telerik.com/devtools/aspnet-ajax/controls/grid/rows/accessing-cells-and-rows
  
Regards,
Pavlina
Telerik
Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Feedback Portal and vote to affect the priority of the items
0
Danilo
Top achievements
Rank 1
answered on 21 Jul 2015, 06:21 AM

Hi Pavlina,

Thanks for your explanation. It works fine.

Regards,
Danilo

Tags
Grid
Asked by
Danilo
Top achievements
Rank 1
Answers by
Maria Ilieva
Telerik team
Danilo
Top achievements
Rank 1
Pavlina
Telerik team
Share this question
or