Raddropdown list selected value not getting bind

7 posts, 0 answers
  1. Tanvi
    Tanvi avatar
    8 posts
    Member since:
    Jan 2014

    Posted 22 Jan 2014 Link to this post

    i have two rad dropdownlists. onselectindex of first dropdown...the second one is getting filled. but the issue over here is:
    second one is not getting filled properly and in edit mode the already present value of the second dropdown is not shown.
    Please help...
    <%--  category--%>
                                            <telerik:GridTemplateColumn DataField="category" HeaderText="Category" SortExpression="category"
                                                UniqueName="category">
                                                <EditItemTemplate>
                                                    <telerik:RadDropDownList runat="server" ID="RadDropDownList3" DataTextField="category"
                                                        AutoPostBack="true" DataValueField="category" DefaultMessage="Select Category" SelectedValue='<%#Bind("category") %>'
                                                        OnSelectedIndexChanged="category_SelectedIndexChanged">
                                                        <Items>
                                                            <telerik:DropDownListItem Text="India" Value='India' />
                                                            <telerik:DropDownListItem Text="International" Value='International' />
                                                        </Items>
                                                    </telerik:RadDropDownList>
                                                    <%--  <telerik:RadDropDownList ID="RadDropDownList6" runat="server">
                                                    </telerik:RadDropDownList>--%>
                                                </EditItemTemplate>
                                                <ItemTemplate>
                                                    <asp:Label ID="lblcategory" runat="server" Text='<%# Bind("category")%>'></asp:Label>
                                                </ItemTemplate>
                                            </telerik:GridTemplateColumn>
                                            <%-- sub category--%>
                                            <telerik:GridTemplateColumn DataField="sub_category" HeaderText="Sub Category" SortExpression="sub_category"
                                                UniqueName="sub_category">
                                                <EditItemTemplate>
                                                    <telerik:RadDropDownList runat="server" ID="RadDropDownList5" DataTextField="sub_category" SelectedValue='<%#Bind("sub_category") %>'
                                                        AutoPostBack="true" DataValueField="sub_category" DefaultMessage="Select Sub Category" RenderMode="Classic">
                                                    </telerik:RadDropDownList>
                                                     <asp:HiddenField ID="hddlsubcategory" runat="server" Value='<%# Bind("sub_category") %>' />
                                                </EditItemTemplate>
                                                <ItemTemplate>
                                                    <asp:Label ID="Label5" runat="server" Text='<%# Bind("sub_category")%>'></asp:Label>
                                                </ItemTemplate>
                                            </telerik:GridTemplateColumn>
     
     
     
    and the code behind for this is:
     
     
    protected void category_SelectedIndexChanged(object sender, DropDownListEventArgs e)
        {
     
     
            RadDropDownList RadDropDownList3 = sender as RadDropDownList;
            GridEditFormItem item = RadDropDownList3.NamingContainer as GridEditFormItem;
            //Use GridDataItem when dropdown is in item template
            //GridDataItem item = RadDropDownList3.NamingContainer as GridDataItem;
            RadDropDownList ddlsubcategory = item.FindControl("RadDropDownList5") as RadDropDownList;
     
            if (RadDropDownList3.SelectedValue == "India")
            {
                //FillCat1();
                ddlsubcategory.ClearSelection();
                ddlsubcategory.Items.Add(new DropDownListItem("North", "North"));
                ddlsubcategory.Items.Add(new DropDownListItem("East", "East"));
                ddlsubcategory.Items.Add(new DropDownListItem("West", "West"));
                ddlsubcategory.Items.Add(new DropDownListItem("South", "South"));
     
            }
            else if (RadDropDownList3.SelectedValue == "International")
            {
                ddlsubcategory.ClearSelection();
                ddlsubcategory.Items.Add(new DropDownListItem("Asia", "Asia"));
                ddlsubcategory.Items.Add(new DropDownListItem("Africa", "Africa"));
                ddlsubcategory.Items.Add(new DropDownListItem("Australia", "Australia"));
                ddlsubcategory.Items.Add(new DropDownListItem("America", "America"));
     
     
            }
     
     
     
        }
     
      protected void gvDetails_ItemDataBound(object sender, GridItemEventArgs e)
        {
     
            if (e.Item is GridEditFormItem && e.Item.IsInEditMode)
            {
                GridEditFormItem item = (GridEditFormItem)e.Item;
                //string value = item.GetDataKeyValue("ID").ToString();
                RadDropDownList sub_category = item.FindControl("RadDropDownList5") as RadDropDownList;
                HiddenField hsubcategory = item.FindControl("hddlsubcategory") as HiddenField;
     
                if (sub_category != null && hsubcategory != null)
                {
                    sub_category.SelectedValue = hsubcategory.Value;
                }
            }
     
     
        }
        
        
  2. Princy
    Princy avatar
    17421 posts
    Member since:
    Mar 2007

    Posted 22 Jan 2014 Link to this post

    Hi Tanvi,

    Please try to set the DataSourceID for the 'RadDropDownList5' to show the  present value from the database as follows.

    ASPX:
    <telerik:RadDropDownList runat="server" ID="RadDropDownList5" DataTextField="CustomerID"
                DataSourceID="SqlDataSource1" DataValueField="CustomerID" DefaultMessage="Select"
                RenderMode="Classic" SelectedValue='<%#Bind("CustomerID") %>'>
    </telerik:RadDropDownList>
    <asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:NorthwindConnectionString %>"
        SelectCommand="SELECT top 10 [OrderID], [CustomerID], [ShipAddress],[OrderDate] FROM [Orders]">
    </asp:SqlDataSource>

    Let me know if you have any concern.
    Thanks,
    Princy.
  3. UI for ASP.NET Ajax is Ready for VS 2017
  4. Tanvi
    Tanvi avatar
    8 posts
    Member since:
    Jan 2014

    Posted 22 Jan 2014 Link to this post

    i have to set static values in the dropdownlist...how can i provide datasourceid to it???
  5. Princy
    Princy avatar
    17421 posts
    Member since:
    Mar 2007

    Posted 23 Jan 2014 Link to this post

    Hi Tanvi,

    The ItemDataBound event of RadGrid will not fire after the SelectedIndexChanged event of RadDropDownList. So you can select an item of the second DropDownList on first DropDownList SelectedIndexChanged event itself. Since you are using DataTextField and DataValueField I have mistakenly give you the DataSourceID. In your scenario there is no need of this fields. Please take a look into the sample code.

    ASPX:
    <telerik:GridTemplateColumn DataField="sub_category" HeaderText="Sub Category" SortExpression="sub_category"
        UniqueName="sub_category">
        <EditItemTemplate>
            <telerik:RadDropDownList runat="server" ID="RadDropDownList5" DefaultMessage="Select Sub Category"
                RenderMode="Classic">
            </telerik:RadDropDownList>
            <asp:HiddenField ID="hddlsubcategory" runat="server" Value='<%# Bind("sub_category") %>' />
        </EditItemTemplate>
        <ItemTemplate>
            <asp:Label ID="Label5" runat="server" Text='<%# Bind("sub_category")%>'></asp:Label>
        </ItemTemplate>
    </telerik:GridTemplateColumn>

    C#:
    protected void category_SelectedIndexChanged(object sender, DropDownListEventArgs e)
    {
        RadDropDownList RadDropDownList3 = sender as RadDropDownList;
        GridEditFormItem item = RadDropDownList3.NamingContainer as GridEditFormItem;
        HiddenField hsubcategory = item.FindControl("hddlsubcategory") as HiddenField;
        RadDropDownList ddlsubcategory = item.FindControl("RadDropDownList5") as RadDropDownList;
        if (RadDropDownList3.SelectedValue == "India")
        {
            ddlsubcategory.Items.Add(new DropDownListItem("North", "North"));
            ddlsubcategory.Items.Add(new DropDownListItem("East", "East"));
            ddlsubcategory.Items.Add(new DropDownListItem("West", "West"));
            ddlsubcategory.Items.Add(new DropDownListItem("South", "South"));
            ddlsubcategory.FindItemByValue(hsubcategory.Value).Selected = true;
     
        }
        else if (RadDropDownList3.SelectedValue == "International")
        {
            ddlsubcategory.Items.Add(new DropDownListItem("Asia", "Asia"));
            ddlsubcategory.Items.Add(new DropDownListItem("Africa", "Africa"));
            ddlsubcategory.Items.Add(new DropDownListItem("Australia", "Australia"));
            ddlsubcategory.Items.Add(new DropDownListItem("America", "America"));
        }
    }

    Thanks,
    Princy.

  6. Tanvi
    Tanvi avatar
    8 posts
    Member since:
    Jan 2014

    Posted 23 Jan 2014 Link to this post

    it works fine when u select india...but when u select "international". it shows an error. and this solution works on selected index.
    but i want it to work when i press the edit mode, both the dropdown lists should contain their respective current values.
    can u please provide a solution for that. that is my actual requirement. where should i add the code.
  7. Princy
    Princy avatar
    17421 posts
    Member since:
    Mar 2007

    Posted 24 Jan 2014 Link to this post

    Hi Tanvi,

    Try clearing the RadDropDownList selection in SelectedIndexChanged event as shown below for removing the error.

    C#:
    protected void category_SelectedIndexChanged(object sender, DropDownListEventArgs e)
    {
        RadDropDownList RadDropDownList3 = sender as RadDropDownList;
        GridEditFormItem item = RadDropDownList3.NamingContainer as GridEditFormItem;
        HiddenField hsubcategory = item.FindControl("hddlsubcategory") as HiddenField;
        RadDropDownList ddlsubcategory = item.FindControl("RadDropDownList5") as RadDropDownList;
        if (RadDropDownList3.SelectedValue == "India")
        {
            ddlsubcategory.ClearSelection();
            ddlsubcategory.Items.Add(new DropDownListItem("North", "North"));
            ddlsubcategory.Items.Add(new DropDownListItem("East", "East"));
            ddlsubcategory.Items.Add(new DropDownListItem("West", "West"));
            ddlsubcategory.Items.Add(new DropDownListItem("South", "South"));
            ddlsubcategory.FindItemByValue(hsubcategory.Value).Selected = true;
        }
        else if (RadDropDownList3.SelectedValue == "International")
        {
            ddlsubcategory.ClearSelection();
            ddlsubcategory.Items.Add(new DropDownListItem("Asia", "Asia"));
            ddlsubcategory.Items.Add(new DropDownListItem("Africa", "Africa"));
            ddlsubcategory.Items.Add(new DropDownListItem("Australia", "Australia"));
            ddlsubcategory.Items.Add(new DropDownListItem("America", "America"));
            ddlsubcategory.FindItemByValue(hsubcategory.Value).Selected = true;
        }
    }

    In order to show the selected value in 'RadDropDownList3' once the RadGrid in edit mode, try setting SelectedValue property as shown below.

    ASPX:
    <telerik:RadDropDownList runat="server" ID="RadDropDownList3" DataTextField="OrderID"
                AutoPostBack="true" DataValueField="OrderID" DefaultMessage="Select Category"
                SelectedValue='<%#Bind("category") %>' OnSelectedIndexChanged="category_SelectedIndexChanged">
                <Items>
                    <telerik:DropDownListItem Text="India" Value='India' Selected="true" />
                    <telerik:DropDownListItem Text="International" Value='International' />
                </Items>
    </telerik:RadDropDownList>

    when Grid is in edit mode, you can populate the second DropDownList and show the current value in itemDataBound as shown below.

    C#:
    protected void RadGrid1_ItemDataBound(object sender, GridItemEventArgs e)
    {
        if (e.Item is GridEditableItem && e.Item.IsInEditMode)
        {
            //excute when the grid is in edit mode
            GridEditableItem item = (GridEditableItem)e.Item;
            RadDropDownList list1 = (RadDropDownList)item.FindControl("RadDropDownList3");
            HiddenField hsubcategory = item.FindControl("hddlsubcategory") as HiddenField;
            string text = list1.SelectedItem.Text; // access the selected item of the RadDrioDownList3
            if (text == "India")
            {
                //if the selected text is india it RadDropDownList will bind with the corresponding value
                RadDropDownList list2 = (RadDropDownList)item.FindControl("RadDropDownList5");
                list2.Items.Add(new DropDownListItem("North", "North"));
                list2.Items.Add(new DropDownListItem("East", "East"));
                list2.Items.Add(new DropDownListItem("West", "West"));
                list2.Items.Add(new DropDownListItem("South", "South"));
                list2.FindItemByValue(hsubcategory.Value).Selected = true;
            }
            else
            {
                RadDropDownList list2 = (RadDropDownList)item.FindControl("RadDropDownList5");
                list2.Items.Add(new DropDownListItem("Asia", "Asia"));
                list2.Items.Add(new DropDownListItem("Africa", "Africa"));
                list2.Items.Add(new DropDownListItem("Australia", "Australia"));
                list2.Items.Add(new DropDownListItem("America", "America"));
                list2.FindItemByValue(hsubcategory.Value).Selected = true;
            }
        }
    }

    Thanks,
    Princy.
  8. Tanvi
    Tanvi avatar
    8 posts
    Member since:
    Jan 2014

    Posted 24 Jan 2014 Link to this post

    The solution works perfectly..!!!! Thanks!!!
Back to Top
UI for ASP.NET Ajax is Ready for VS 2017