I'm getting this error on DataBind(), and I don't know why since there shouldn't be anything selected.

DdState.Items.Clear();
DdState.DataSource = UsStates;
DdState.DataTextField = "Title";
DdState.DataValueField = "Title";           
DdState.Items.Insert(0, String.Empty);
if (DdState.SelectedItem != null)
{
    DdState.SelectedItem.Selected = false;
}
DdState.DataBind();

private IEnumerable<IStateItem> UsStates
{
    get
    {
        var statesFolder = _sitecoreService.GetItem<ISitecoreItem>(ItemReference.BcsUs_ProductData_States.Guid);
        if (statesFolder == null)
            return new List<IStateItem>();

        List<IStateItem> usStates = _sitecoreService.QueryChildren<IStateItem>(statesFolder).OrderBy(s => s.Title).ToList();
        return usStates;
    }
}

I tried putting in DdState.SelectedIndex = 0 before the DataBind(), but then I got an error that the selected index did not exist. What's going on?

2

There are 2 answers

0
blaze_125 On

Could there be something wrong with your IStateItem class? I copy/pasted your code in a new asp.net application, made my own IStateItem class and it works.

using System;
using System.Collections.Generic;

namespace TestIt
{
    public partial class Form1 : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            FillTheList();
        }

        private void FillTheList()
        {
            ddl_TheList.Items.Clear();
            ddl_TheList.DataSource = UsStates;
            ddl_TheList.DataTextField = "statename";
            ddl_TheList.DataValueField = "stateStatus";
            //ddl_TheList.Items.Insert(0, String.Empty);
            ddl_TheList.DataBind();
            ddl_TheList.SelectedIndex = 0;
        }

        private IEnumerable<IStateItem> UsStates
        {
            get
            {
                List<IStateItem> usStates = new List<IStateItem>();
                for (int i = 0; i < 10; i++)
                {
                    usStates.Add(new IStateItem { statename = "state #" + i, stateStatus = "cool state bro" });
                }
                return usStates;
            }
        }
    }

    public class IStateItem
    {
        public string statename { get; set; }
        public string stateStatus { get; set; }
    }


}
4
Burtscher_Florian On

If the DataSource is a list its much easier to implement. So just "convert" the UsStates IEnumerable to a List an then add it to the data source.

DdState.DataSource = UsStates.ToList();

Then choose the property of a list item as binding.

OR

public Form1()
    {

        InitializeComponent();

        DdState.Items.Clear();
        DdState.DataSource = UsStates;
        DdState.DisplayMember = "Statename";
        DdState.SelectedIndex = 0;
    }

    private List<IStateItem> UsStates
    {
        get
        {

            List<IStateItem> usStates = new List<IStateItem>();

            usStates.Add(new IStateItem("California","status1"));
            usStates.Add(new IStateItem("Ohio", "status3"));
            return usStates;
        }
    }

    private class IStateItem
    {
        public IStateItem(string statename, string stateStatus)
        {
            Statename = statename;
            StateStatus = stateStatus;
        }
        public string Statename { get; set; }
        public string StateStatus { get; set; }
    }