RadGrid.FindControl does not find my DropDownList

5 posts, 1 answers
  1. Danilo
    Danilo avatar
    47 posts
    Member since:
    Sep 2012

    Posted 10 Jul 2015 Link to this post

    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

  2. Maria Ilieva
    Admin
    Maria Ilieva avatar
    4017 posts

    Posted 13 Jul 2015 Link to this post

    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
  3. Danilo
    Danilo avatar
    47 posts
    Member since:
    Sep 2012

    Posted 13 Jul 2015 in reply to Maria Ilieva Link to this post

    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?

  4. Answer
    Pavlina
    Admin
    Pavlina avatar
    6187 posts

    Posted 16 Jul 2015 Link to this post

    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
  5. Danilo
    Danilo avatar
    47 posts
    Member since:
    Sep 2012

    Posted 21 Jul 2015 in reply to Pavlina Link to this post

    Hi Pavlina,

    Thanks for your explanation. It works fine.

    Regards,
    Danilo

Back to Top