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

Unable to find ASP dropdown inside EditItemTemplate using FIndControl

6 Answers 390 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Amit
Top achievements
Rank 1
Amit asked on 09 Dec 2014, 12:46 PM
Hi,

I need to bind my dropdowns at server end with the list retrieved from service.
I tried to follow demo provided in http://demos.telerik.com/aspnet-ajax/grid/examples/data-editing/batch-editing/defaultcs.aspx

hard luck is; in demo the datasource is defined in designer itself but I need to bind the ASP dropdownlist dynamically with the data retrieved through services in form of datatable/ lists.

I tried following code block on ItemDataBound, ItemUpdate and ItemEdit events but no luck.

Designer code:

<rad:RadGrid ID="grdAssessmentConfig" runat="server" CssClass="table_heading" AllowPaging="false"
            AllowSorting="True" PagerStyle-AlwaysVisible="false" AllowMultiRowSelection="False"
            AllowMultiRowEdit="false" AllowAutomaticInserts="True"
            AllowAutomaticUpdates="True" OnBatchEditCommand="grdAssessmentConfig_ItemEdit" 
            OnItemDataBound="grdAssessmentConfig_ItemDataBound" EnableTheming="true" MasterTableView-AlternatingItemStyle-BackColor="#eaeff5"
            AutoGenerateColumns="false" GridLines="Vertical" FilterItemStyle-CssClass="RadGridFilter">
            <%--  <PagerStyle Mode="NextPrevAndNumeric" Width="100%" AlwaysVisible="true" />--%>
            <GroupingSettings CaseSensitive="false" />
            <MasterTableView HorizontalAlign="Right" DataSourcePersistenceMode="ViewState" CommandItemDisplay="None"
                EditMode="Batch">
                <%--<CommandItemSettings ShowExportToCsvButton="true"></CommandItemSettings>--%>
                <BatchEditingSettings EditType="Cell" OpenEditingEvent="Click"  />
                <Columns>
                    <rad:GridTemplateColumn HeaderText="Assess" UniqueName="Assessment"
                        DataField="Assessment">
                        <ItemTemplate>
                            <asp:Label runat="server" ID="lblAssess" Text='<%# Eval("column1")%>'></asp:Label>
                            <asp:HiddenField ID="hdnAssess" runat="server" Value='<%#Eval("column1")%>' />
                        </ItemTemplate>
                        <EditItemTemplate>
                            <asp:DropDownList ID="ddlAssesment" runat="server" CssClass="rad_Width">
                            </asp:DropDownList>
                        </EditItemTemplate>
                    </rad:GridTemplateColumn>
                    <rad:GridTemplateColumn HeaderText="Care">
                        <ItemTemplate>
                            <asp:HiddenField runat="server" ID="hdnCare" Value='<%#Eval("Column2")%>' />
                            <asp:Label runat="server" ID="lblCare" Text='<%#Eval("Column2")%>'></asp:Label>
                        </ItemTemplate>
                        <EditItemTemplate>
                            <asp:DropDownList ID="ddlCare" runat="server" DataSourceID="ds1" CssClass="rad_Width">
                            </asp:DropDownList>
                        </EditItemTemplate>
                    </rad:GridTemplateColumn>
                   <%-- <rad:GridEditCommandColumn UniqueName="EditCommandColumn">
                    </rad:GridEditCommandColumn>--%>
                </Columns>
            </MasterTableView>
            <%--<ClientSettings AllowKeyboardNavigation="true"></ClientSettings>--%>
        </rad:RadGrid>

 Code Behind:

   if (e.Item is GridDataItem)
                {

                    DropDownList ddlAssess = (DropDownList)e.Item.FindControl("ddlAssesment");

                    // GridEditableItem grdEdit = 

                    DropDownList ddlCare = (DropDownList)e.Item.FindControl("ddlCare");
                    HiddenField hdAssessment = (HiddenField)e.Item.FindControl("hdnAssess");
                    HiddenField hdCare = (HiddenField)e.Item.FindControl("hdnCare");
                    Label lblCare = (Label)e.Item.FindControl("lblCare");
                    Label lblAssess = (Label)e.Item.FindControl("lblAssess");


  Code successfully returns Label and Hidden controls as they are in ItenTemplate but not the dropdownlist.
I am unable to tress what I am missing.

Any help is appreciated.

Thanks






6 Answers, 1 is accepted

Sort by
0
Eric
Top achievements
Rank 1
answered on 09 Dec 2014, 07:07 PM
Hi,
You will only be able to access that dropdown when your grid is in Edit mode, and the EditTemplate is active. In your ItemDataBound event you can test to see if the item is an edit item or a regular item.

if (e.Item is GridDataItem)
{
    //your other stuff
}
else if (e.Item is GridEditableItem)
{
    //find and bind your dropdown here
}


You can also check the item type by doing this
if (e.Item.ItemType == GridItemType.EditItem)
{
}
0
Amit
Top achievements
Rank 1
answered on 10 Dec 2014, 12:25 PM
Thanks Eric,

I tried both the options but was unable to invoke code behind method. I am using Batch Edit mode for grid and seems Batch edit mode does not supports server side binding.

I have attached source code of Application here, I am using as POC for this.

Designer:
  <div>
    <asp:ScriptManager ID="scrpt" runat="server"></asp:ScriptManager>
             <telerik:radgrid id="grd1" runat="server" OnBatchEditCommand="grd_Edit" >
        <MasterTableView CommandItemDisplay="TopAndBottom" BatchEditingSettings-OpenEditingEvent="Click" EditFormSettings-EditFormType="AutoGenerated"  ViewStateMode="Enabled"  
                HorizontalAlign="NotSet" EditMode="Batch" AutoGenerateColumns="False">
                <BatchEditingSettings EditType="Cell" />
                <Columns>
                <telerik:GridTemplateColumn>
                <ItemTemplate>
                <%# Eval("QuestionID")%>
                </ItemTemplate>
                <EditItemTemplate>
                <telerik:RadDropDownList runat="server" ID="ddltest"></telerik:RadDropDownList>
                <%--<asp:DropDownList runat="server" ID="ddltest"></asp:DropDownList>--%>
                </EditItemTemplate>
                </telerik:GridTemplateColumn>
                <telerik:GridTemplateColumn>
                <ItemTemplate>
                <%# Eval("Agency")%>
                </ItemTemplate>
                <EditItemTemplate>
                <asp:DropDownList runat="server" ID="ddlrdr"></asp:DropDownList>
                </EditItemTemplate>
                </telerik:GridTemplateColumn>
                <telerik:GridBoundColumn DataField="Provider" > </telerik:GridBoundColumn>
                <telerik:GridTemplateColumn HeaderText="Response" > 
                <ItemTemplate>
                 <%# Eval("Response")%>
                </ItemTemplate>
                <EditItemTemplate>
                <asp:TextBox runat="server" ID="txt1"></asp:TextBox>
                </EditItemTemplate>
                </telerik:GridTemplateColumn>
                </Columns>
                </MasterTableView>
        </telerik:radgrid>
    </div>

 Code Behind:

 namespace RadGridPOC
{
    public partial class _Default : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            FillGrid();


            Dictionary<int, string> dcTest = new Dictionary<int, string>();
            dcTest.Add(23, "Question1");
            dcTest.Add(24, "Question4");
            dcTest.Add(25, "Question3");
            dcTest.Add(26, "Question2");


            Content mhp = (Content)Master.FindControl("BodyContent");


            //foreach (Telerik.Web.UI.GridDataItem dr in grd1.Items)
            //{
            //    if (dr is GridDataItem )
            //    {
                   
            //        RadDropDownList ddr = (RadDropDownList)mhp.FindControl("ddltest");

            //        ddr.DataSource = dcTest;
            //        ddr.DataValueField = "Key";
            //        ddr.DataTextField = "Value";
            //        ddr.DataBind();
            //    }
            //}
        }

       public void FillGrid()
        {
          DataTable dtRouting = new DataTable();
          DataRow drRouting;
            dtRouting.Columns.Add("QuestionID", typeof(String));
            dtRouting.Columns.Add("QuestionValue", typeof(String));
            dtRouting.Columns.Add("Response", typeof(String));
            dtRouting.Columns.Add("Agency", typeof(String));
            dtRouting.Columns.Add("Provider", typeof(String));

            drRouting = dtRouting.NewRow();

            drRouting["QuestionID"] = "Question1";
            drRouting["Response"] = "Response 1";
            drRouting["Agency"] = "Agency 1";
            drRouting["Provider"] = "Provider 1";
            drRouting["QuestionValue"] = "1234";
           
            dtRouting.Rows.Add(drRouting);

            drRouting = dtRouting.NewRow();

            drRouting["QuestionID"] = "Question2";
            drRouting["Response"] = "Response 2";
            drRouting["Agency"] = "Agency 2";
            drRouting["Provider"] = "Provider 2";
            drRouting["QuestionValue"] = "1233";

            dtRouting.Rows.Add(drRouting);

            grd1.DataSource = dtRouting;
            grd1.DataBind();
        }

       protected void grd_Edit(object sender, Telerik.Web.UI.GridBatchEditingEventArgs e)
       {
           if (e.Item.ItemType == GridItemType.EditItem)
           {
               GridEditableItem it = (GridEditableItem)e.Item;

               RadDropDownList ddr = (RadDropDownList)it.FindControl("ddltest");

           }
       }

       protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
       {

       }
    }
}


0
Eric
Top achievements
Rank 1
answered on 10 Dec 2014, 02:21 PM
Ah, yes... I see what you mean now. It does appear that this isn't supported at the moment. But, may I suggest an alternate approach? You could modify your DropDownLists to use a data source: In my test, I used SelectMethod, and it seems to work.

ASPX
<asp:DropDownList runat="server" ID="ddltest" SelectMethod="GetQuestions" DataTextField="Text" DataValueField="Id" />

CS
public IList<object> GetQuestions()
    {
 
        var data = new[] {  new{Id = 23, Text="Question1"},
                            new{Id = 24, Text="Question4"},
                            new{Id = 25, Text="Question3"},
                            new{Id = 26, Text="Question2"}};
 
        return data;
    }

I tried this with the code you provided and it worked. For some reason, during the grid's creation, the item bound events aren't called for EditTemplate, even though the controls are created ahead of time. I also tried looking into the ItemCreated event, but no luck there.

It seems also that the Demo on Telerik's site dodges this issue by using a DataSourceID on their DropDownLists... presumably because of the same issue. You just can't get access to it from code-behind.
0
Lee
Top achievements
Rank 1
answered on 20 Oct 2015, 09:38 PM
So I"m having the same problem. Are you saying that you cannot use FindControl() on any EditItemTemplate control?  And one workaround is to use a sqldatasource (embedded db connection in the .aspx page) to handle this as opposed to the FindControl method inside of ItemDataBound, for instance?
0
Eyup
Telerik team
answered on 23 Oct 2015, 08:55 AM
Hi Lee,

Which EditMode do you use? Generally, the FindControl() method should work with templates:
http://docs.telerik.com/devtools/aspnet-ajax/controls/grid/rows/accessing-cells-and-rows#accessing-controls-in-editinsert-mode


If you are using Batch editing mode, please note that it is different than other modes. You can find a detailed explanation in the following section:
http://docs.telerik.com/devtools/aspnet-ajax/controls/grid/rows/accessing-cells-and-rows#accessing-controls-in-batch-edit-mode


Hope this helps.

Regards,
Eyup
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
Lee
Top achievements
Rank 1
answered on 20 Nov 2015, 05:54 PM
It's batch. So I need to find the control/find the value in javascript...that seems doable.
Tags
Grid
Asked by
Amit
Top achievements
Rank 1
Answers by
Eric
Top achievements
Rank 1
Amit
Top achievements
Rank 1
Lee
Top achievements
Rank 1
Eyup
Telerik team
Share this question
or