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

Can't FindControl

4 Answers 155 Views
Grid
This is a migrated thread and some comments may be shown as answers.
tim
Top achievements
Rank 1
tim asked on 05 Mar 2010, 02:20 PM
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,







4 Answers, 1 is accepted

Sort by
0
Daniel
Telerik team
answered on 05 Mar 2010, 04:59 PM
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.
0
tim
Top achievements
Rank 1
answered on 30 Mar 2010, 04:02 PM
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.
0
Shinu
Top achievements
Rank 2
answered on 31 Mar 2010, 07:04 AM

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.

0
JazzBox
Top achievements
Rank 1
answered on 30 Oct 2017, 08:52 PM
did you figure this out?
Tags
Grid
Asked by
tim
Top achievements
Rank 1
Answers by
Daniel
Telerik team
tim
Top achievements
Rank 1
Shinu
Top achievements
Rank 2
JazzBox
Top achievements
Rank 1
Share this question
or