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

unable to use FindControl to get asp control set under ItemTemplate

2 Answers 74 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Alice Leong
Top achievements
Rank 1
Alice Leong asked on 31 Mar 2010, 07:41 AM
Hi All,

This is my first time using Telerik in ASP.Net project.
In my project, I have used TelerikGrid for displaying and editing data. Please refer to code below:

<telerik:RadGrid ID="rgAddData" GridLines="None" runat="server" AllowAutomaticDeletes="True"
     AllowAutomaticInserts="True" AllowAutomaticUpdates="True" AllowPaging="True" PageSize="10" EnableLinqExpressions="false"
     AutoGenerateColumns="False" OnItemUpdated="rgAddData_ItemUpdated" OnItemDeleted="rgAddData_ItemDeleted"
     OnItemInserted="rgAddData_ItemInserted" OnItemCommand="rgAddData_ItemCommand">
     <PagerStyle Mode="NextPrevAndNumeric" />
             <MasterTableView Width="100%" EditMode="InPlace"
                                CommandItemSettings-AddNewRecordImageUrl="../images/img_Add.png"
                                CommandItemSettings-AddNewRecordText = "Add Data"                       
                                CommandItemSettings-RefreshText = ""
                                CommandItemSettings-RefreshImageUrl="../images/img_Blank.png"
                                ShowHeadersWhenNoRecords="true" ShowHeader="true"
                                CommandItemDisplay="Top" DataKeyNames="DataID"
                                AutoGenerateColumns="False"
                                HeaderStyle-Font-Bold="true">
                                <Columns>                                                             
                                    <telerik:GridDropDownColumn HeaderText="Client" UniqueName="Client" ColumnEditorID="DropDownStyle">
                                    </telerik:GridDropDownColumn>                                                            
                                    
                                    <telerik:GridTemplateColumn HeaderText="Age" UniqueName="Age">
                                        <ItemTemplate>
                                            <asp:DropDownList ID="ddlAge" runat="server" Style="width:110px;"></asp:DropDownList>
                                        </ItemTemplate>
                                    </telerik:GridTemplateColumn>                    
                            
                                    <telerik:GridEditCommandColumn ButtonType="ImageButton" UniqueName="EditCommandColumn">
                                    </telerik:GridEditCommandColumn>
                                    
    
                                    <telerik:GridButtonColumn ConfirmText="Delete this data?" ConfirmDialogType="RadWindow"
                                        ConfirmTitle="Delete" ButtonType="ImageButton" CommandName="Delete" Text="Delete"
                                        UniqueName="DeleteColumn">
                                        <ItemStyle HorizontalAlign="Center" CssClass="addCassetteImageButton" />
                                    </telerik:GridButtonColumn>
                                </Columns>
                
                                <EditFormSettings ColumnNumber="2" CaptionDataField="DataID" CaptionFormatString="Edit cassette {0}"> </EditFormSettings>
                            </MasterTableView>          
                        </telerik:RadGrid>
                                                
                        <telerik:GridDropDownListColumnEditor ID="DropDownStyle" runat="server" DropDownStyle-Width="110px" />
                        
                        <telerik:RadWindowManager ID="RadWindowManager1" runat="server"></telerik:RadWindowManager> 
---------------------------------------------------------------------------------------------------------------
I have an asp:dropdownlist control declared under the ItemTemplate. This drop down list will then bind with the data return from the file from another c# project. Below is the code:

 public static List<KeyValuePair<int, string>> GetAgeList()
        {
            List<KeyValuePair<int, string>> ageList = new List<KeyValuePair<int, string>>();
            for (int i = 0; i < 10; i++)
            {
                ageList.Add(new KeyValuePair<int, string>(i, i + " yrs"));
            }
            return ageList;
        }

--------------------------------------------------------------------------

Then in my C# code, I want to use findControl to get the "ddlAge" in order to bind with the return values from the above codes.
Below is my code:

protected void rgAddCassette_ItemCommand(object source, Telerik.Web.UI.GridCommandEventArgs e)
    {             
        ((DropDownList)e.Item.FindControl("ddlAge")).DataSource = Services.ListOfValues.GetAgeList();
        ((DropDownList)e.Item.FindControl("ddlAge")).DataBind();
    }

---------------------------------------------
However, I got this error cannot find the control.
Could anyone please do help me on how should I use the FindControl to get the asp dropdownlist control that I declared under ItemTemplate?
In my project, there are 3 RadGrid with similar settings, where I've added the asp drop down list under itemtemplate and bind with the value list return from other project.

Thanks for the help!




2 Answers, 1 is accepted

Sort by
0
Princy
Top achievements
Rank 2
answered on 31 Mar 2010, 08:44 AM
Hi,

Since you are using the DropDownList inside ItemTemplate you can use ItemDataBound event to populate it. This event is fires for items of type: GridDataItem, GridEditFormItem, GridHeaderItem, GridPagerItem, GridFooterItem, therefore you need to check for whether the item is GridDataItem in the event.

See more on this in help document ItemDataBound Event

Try the following code:
 
protected void RadGrid3_ItemDataBound(object sender, Telerik.Web.UI.GridItemEventArgs e) 
    if (e.Item is GridDataItem) 
    { 
        ((DropDownList)e.Item.FindControl("ddlAge")).DataSource = Services.ListOfValues.GetAgeList(); 
        ((DropDownList)e.Item.FindControl("ddlAge")).DataBind(); 
    } 

Princy
0
Alice Leong
Top achievements
Rank 1
answered on 31 Mar 2010, 09:14 AM
Hi Princy,

Thanks alot! It works. So, it won't work in ItemCommand...
and thanks for the link!
Tags
Grid
Asked by
Alice Leong
Top achievements
Rank 1
Answers by
Princy
Top achievements
Rank 2
Alice Leong
Top achievements
Rank 1
Share this question
or