10

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 Answers 4

10

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.

Sign up to request clarification or add additional context in comments.

1 Comment

Why do I need to set that field? As I said it works when the ListItem is added directly to the DropDownList.Items-collection. I tried your suggestion and it didn't help. See my updated question.
3

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;
    }
}

1 Comment

Thank you. I think that having "Text" and "Value" as the default properties is a good idea.
3

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).

1 Comment

Thanks for your comment, even if it didn't answer according to his last sentence. I didn't realize that's all I needed to do.
1

"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...

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.