Adding ListItems to a DropDownList from a generic

2019-04-21 04:22发布

I have a this aspx-code: (sample)

<asp:DropDownList runat="server" ID="ddList1"></asp:DropDownList>   

With this codebehind:

List<System.Web.UI.WebControls.ListItem> colors = new List<System.Web.UI.WebControls.ListItem>();
colors.Add(new ListItem("Select Value", "0"));
colors.Add(new ListItem("Red", "1"));
colors.Add(new ListItem("Green", "2"));
colors.Add(new ListItem("Blue", "3"));
ddList1.DataSource = colors;
ddList1.DataBind();

The output looks like this:

<select name="ddList1" id="ddList1">
    <option value="Select Value">Select Value</option>
    <option value="Red">Red</option>
    <option value="Green">Green</option>
    <option value="Blue">Blue</option>
</select>   

My question is: Why did my values (numbers) disappear and the text used as the value AND the text? I know that it works if I use the ddList1.Items.Add(New ListItem("text", "value")) method, but I need to use a generic list as the datasource for other reasons.

4条回答
乱世女痞
2楼-- · 2019-04-21 04:59

If you are building ListItems, you have no need to use DataBind() in the first place.

Just add them to your DropDownList:



ddList1.Items.Add(new ListItem("Select Value", "0"));
ddList1.Items.Add(new ListItem("Red", "1"));
ddList1.Items.Add(new ListItem("Green", "2"));
ddList1.Items.Add(new ListItem("Blue", "3"));

DataBind() is useful when you already have a collection/dataobject (usually a DataTable or DataView) that can be used as a DataSource, by setting the DataTextField and DataValueField (as buyutec wrote).

查看更多
Animai°情兽
3楼-- · 2019-04-21 05:02

"If you are building ListItems, you have no need to use DataBind() in the first place."

Adding directly to the dropdownlist is the easy way (and given the example code the right one) but lets say you have an unordered datasource and you want the list items sorted.

One way of achieving this would be to create a generic list of ListItem and then use the inherited sort method before databinding to the list.

There are many wys to skin a cat...

查看更多
Ridiculous、
4楼-- · 2019-04-21 05:12

And here is the method that performs the data binding. You can exactly see what is going on:

protected internal override void PerformDataBinding(IEnumerable dataSource)
{
    base.PerformDataBinding(dataSource);
    if (dataSource != null)
    {
        bool flag = false;
        bool flag2 = false;
        string dataTextField = this.DataTextField;
        string dataValueField = this.DataValueField;
        string dataTextFormatString = this.DataTextFormatString;
        if (!this.AppendDataBoundItems)
        {
            this.Items.Clear();
        }
        ICollection is2 = dataSource as ICollection;
        if (is2 != null)
        {
            this.Items.Capacity = is2.Count + this.Items.Count;
        }
        if ((dataTextField.Length != 0) || (dataValueField.Length != 0))
        {
            flag = true;
        }
        if (dataTextFormatString.Length != 0)
        {
            flag2 = true;
        }
        foreach (object obj2 in dataSource)
        {
            ListItem item = new ListItem();
            if (flag)
            {
                if (dataTextField.Length > 0)
                {
                    item.Text = DataBinder.GetPropertyValue(obj2, dataTextField, dataTextFormatString);
                }
                if (dataValueField.Length > 0)
                {
                    item.Value = DataBinder.GetPropertyValue(obj2, dataValueField, null);
                }
            }
            else
            {
                if (flag2)
                {
                    item.Text = string.Format(CultureInfo.CurrentCulture, dataTextFormatString, new object[] { obj2 });
                }
                else
                {
                    item.Text = obj2.ToString();
                }
                item.Value = obj2.ToString();
            }
            this.Items.Add(item);
        }
    }
    if (this.cachedSelectedValue != null)
    {
        int num = -1;
        num = this.Items.FindByValueInternal(this.cachedSelectedValue, true);
        if (-1 == num)
        {
            throw new ArgumentOutOfRangeException("value", SR.GetString("ListControl_SelectionOutOfRange", new object[] { this.ID, "SelectedValue" }));
        }
        if ((this.cachedSelectedIndex != -1) && (this.cachedSelectedIndex != num))
        {
            throw new ArgumentException(SR.GetString("Attributes_mutually_exclusive", new object[] { "SelectedIndex", "SelectedValue" }));
        }
        this.SelectedIndex = num;
        this.cachedSelectedValue = null;
        this.cachedSelectedIndex = -1;
    }
    else if (this.cachedSelectedIndex != -1)
    {
        this.SelectedIndex = this.cachedSelectedIndex;
        this.cachedSelectedIndex = -1;
    }
}
查看更多
We Are One
5楼-- · 2019-04-21 05:16

Because DataBind method binds values only if DataValueField property is set. If you set DataValueField property to "Value" before calling DataBind, your values will appear on the markup.

UPDATE: You will also need to set DataTextField property to "Text". It is because data binding and adding items manually do not work in the same way. Databinding does not know the existence of type ListItem and generates markup by evaluating the items in the data source.

查看更多
登录 后发表回答