SelectedValue is empty when binding to a generic list

3 posts, 1 answers
  1. Charles
    Charles avatar
    30 posts
    Member since:
    Feb 2013

    Posted 25 Jun 2013 Link to this post

    Hi,

    I am binding a RadDropDownTree to a Generic List (ListofT) as shown in the example at http://www.telerik.com/help/aspnet-ajax/dropdowntree-databinding-array.html, but the control's SelectedValue property is always empty. Is there a way to get the ID of the selected node from the control? Or is there something different I have to do make the SelectedValue property take on the ID property in the example?

    Thanks.

    Charles
  2. Answer
    Shinu
    Shinu avatar
    17764 posts
    Member since:
    Mar 2007

    Posted 26 Jun 2013 Link to this post

    Hi Charles,

    I checked the documentation link you provided. In the "Data Binding using Generic List", the DataTextField property is only given and they have not specified any DataValueField property and hence you are getting an empty value. For the same scenario, I have created a sample code with the DataValueProperty set. Please check the following code.

    ASPX:
    <telerik:RadDropDownTree ID="RadDropDownTree1" runat="server" Width="350px" DefaultMessage="Select a product from the list">
        <DropDownSettings Width="350px" />
    </telerik:RadDropDownTree>
    <br />
    <br />
    <telerik:RadButton ID="RadButton1" runat="server" Text="Get Selected Value" OnClick="RadButton1_Click">
    </telerik:RadButton>
    <br />
    <br />
    <asp:Label ID="Label1" runat="server"></asp:Label>

    C#:
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            BindToIEnumerable(RadDropDownTree1);
        }
    }
    private static void BindToIEnumerable(RadDropDownTree dropdowntree)
    {
        List<SiteDataItem> siteData = new List<SiteDataItem>();
     
        siteData.Add(new SiteDataItem(1, 0, "Products", "Value 0"));
        siteData.Add(new SiteDataItem(2, 1, "RadControls for ASP.NET Ajax", "Value 1"));
        siteData.Add(new SiteDataItem(3, 1, "RadControls for Silverlight", "Value 2"));
        siteData.Add(new SiteDataItem(4, 2, "RadGrid", "Value 3"));
        siteData.Add(new SiteDataItem(5, 2, "RadScheduler", "Value 4"));
        siteData.Add(new SiteDataItem(6, 2, "RadEditor", "Value 5"));
        siteData.Add(new SiteDataItem(7, 3, "RadGrid", "Value 6"));
        siteData.Add(new SiteDataItem(8, 3, "RadMenu", "Value 7"));
        siteData.Add(new SiteDataItem(9, 3, "RadEditor", "Value 8"));
     
        dropdowntree.DataTextField = "Text";
        dropdowntree.DataValueField = "Value";
        dropdowntree.DataFieldID = "ID";
        dropdowntree.DataFieldParentID = "ParentID";
        dropdowntree.DataSource = siteData;
        dropdowntree.DataBind();
    }
     
    internal class SiteDataItem
    {
        private string _text;
        private string _value;
        private int _id;
        private int _parentId;
     
        public string Text
        {
            get { return _text; }
            set { _text = value; }
        }
     
        public string Value
        {
            get { return _value; }
            set { _value = value; }
        }
     
        public int ID
        {
            get { return _id; }
            set { _id = value; }
        }
     
        public int ParentID
        {
            get { return _parentId; }
            set { _parentId = value; }
        }
     
        public SiteDataItem(int id, int parentId, string text, string value)
        {
            _id = id;
            _parentId = parentId;
            _text = text;
            _value = value;
        }
    }
    protected void RadButton1_Click(object sender, EventArgs e)
    {
        Label1.Text = RadDropDownTree1.SelectedValue.ToString();
    }

    Thanks,
    Shinu.
  3. UI for ASP.NET Ajax is Ready for VS 2017
  4. Charles
    Charles avatar
    30 posts
    Member since:
    Feb 2013

    Posted 26 Jun 2013 Link to this post

    That did the trick. Thanks!

    Charles
Back to Top