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

SelectedValue is empty when binding to a generic list

2 Answers 147 Views
DropDownTree
This is a migrated thread and some comments may be shown as answers.
Charles
Top achievements
Rank 1
Charles asked on 25 Jun 2013, 08:37 PM
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 Answers, 1 is accepted

Sort by
0
Accepted
Shinu
Top achievements
Rank 2
answered on 26 Jun 2013, 05:14 AM
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.
0
Charles
Top achievements
Rank 1
answered on 26 Jun 2013, 12:11 PM
That did the trick. Thanks!

Charles
Tags
DropDownTree
Asked by
Charles
Top achievements
Rank 1
Answers by
Shinu
Top achievements
Rank 2
Charles
Top achievements
Rank 1
Share this question
or