Can't FindControl

5 posts, 0 answers
  1. tim
    tim avatar
    21 posts
    Member since:
    Sep 2008

    Posted 05 Mar 2010 Link to this post

    I'm having problems finding a control in my grid.  I've been stuck on this for a couple of days and have searched the forums, following examples and links to documentation, with no luck.  Here's a snippet from the aspx page.

     <telerik:GridTemplateColumn UniqueName="ddHeader" HeaderText="Header"
       <EditItemTemplate> 
         <asp:DropDownList ID="HeaderDropDown" runat="server" /> 
       </EditItemTemplate> 
     </telerik:GridTemplateColumn> 

    And here's a snippet from code behind.
    private void Grid1_ItemDataBound(object sender, GridItemEventArgs e) 
        { 
            if (e.Item is GridEditableItem) 
            { 
                GridEditableItem item = e.Item as GridEditableItem; 
                Dictionary<string, string> dict = new Dictionary<string, string>(); 
                dict = ReadHeader(); 
                DropDownList list =item.FindControl("HeaderDropDown") as DropDownList; 
                list.DataSource = dict
                list.DataBind(); 
            } 
        } 

    DropDownList list =item.FindControl("HeaderDropDown") as DropDownList;  always returns null.

    What am I not getting here?

    Thanks,







  2. Daniel
    Admin
    Daniel avatar
    4949 posts

    Posted 05 Mar 2010 Link to this post

    Hello Tim,

    You can access the DropDownList only when the corresponding item is in Edit mode. Please try to change your code as shown below:
    protected void RadGrid1_ItemDataBound(object sender, GridItemEventArgs e)
    {
        if (e.Item.IsInEditMode)
        {
            GridEditableItem item = e.Item as GridEditableItem;
            ...
            DropDownList list = item.FindControl("HeaderDropDown") as DropDownList;
            ...
        }
    }

    Best regards,
    Daniel
    the Telerik team

    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 Public Issue Tracking system and vote to affect the priority of the items.
  3. tim
    tim avatar
    21 posts
    Member since:
    Sep 2008

    Posted 30 Mar 2010 Link to this post

    I'm not getting to the code:

     private void Grid1_ItemDataBound(object sender, GridItemEventArgs e) 
        { 
            if (e.Item.IsInEditMode) 
            { 
                GridEditableItem item = e.Item as GridEditableItem; 
                Dictionary<string, string> dict = new Dictionary<string, string>(); 
                dict = ReadHeader(); 
                DropDownList list = item.FindControl("HeaderDropDown") as DropDownList; 
                list.DataSource = dict
                list.DataBind(); 
            } 
        } 

    Do I need to put something in the <telerik:RadGrid ID="Grid1" AutoGenerateColumns="false" runat="server" Skin="Web20" Width="90%"> tag?

    Thanks.
  4. Shinu
    Shinu avatar
    17764 posts
    Member since:
    Mar 2007

    Posted 31 Mar 2010 Link to this post

    Hello Tim,

    The sample code given by Daniel is working as expected when I tried on my end. I am not quite sure about your code. Have you used FormTemplate other than adding EditItemTemplate, which can be a reason for unable to access the DropdownList from code?

    If you could provide us the complete code that you tried, it will be very helpful.

    -Shinu.

  5. JazzBox
    JazzBox avatar
    8 posts
    Member since:
    Jul 2015

    Posted 30 Oct 2017 in reply to tim Link to this post

    did you figure this out?
Back to Top