Unable to find ASP dropdown inside EditItemTemplate using FIndControl

7 posts, 0 answers
  1. Amit
    Amit avatar
    2 posts
    Member since:
    Feb 2012

    Posted 09 Dec 2014 Link to this post

    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






  2. Eric
    Eric avatar
    16 posts
    Member since:
    Feb 2013

    Posted 09 Dec 2014 in reply to Amit Link to this post

    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)
    {
    }
  3. UI for ASP.NET Ajax is Ready for VS 2017
  4. Amit
    Amit avatar
    2 posts
    Member since:
    Feb 2012

    Posted 10 Dec 2014 in reply to Eric Link to this post

    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)
           {

           }
        }
    }


  5. Eric
    Eric avatar
    16 posts
    Member since:
    Feb 2013

    Posted 10 Dec 2014 in reply to Amit Link to this post

    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.
  6. Lee
    Lee avatar
    4 posts
    Member since:
    Feb 2015

    Posted 20 Oct 2015 Link to this post

    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?
  7. Eyup
    Admin
    Eyup avatar
    3015 posts

    Posted 23 Oct 2015 Link to this post

    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
  8. Lee
    Lee avatar
    4 posts
    Member since:
    Feb 2015

    Posted 20 Nov 2015 in reply to Eyup Link to this post

    It's batch. So I need to find the control/find the value in javascript...that seems doable.
Back to Top
UI for ASP.NET Ajax is Ready for VS 2017