代码之家  ›  专栏  ›  技术社区  ›  Espo

将ListItems从常规列表添加到DropDownList

  •  10
  • Espo  · 技术社区  · 16 年前

    我有一个aspx代码:(示例)

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

    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();
    

    输出如下所示:

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

    ddList1.Items.Add(New ListItem("text", "value")) 方法,但出于其他原因,我需要使用泛型列表作为数据源。

    4 回复  |  直到 16 年前
        1
  •  10
  •   Serhat Ozgel    16 年前

        2
  •  3
  •   Serhat Ozgel    16 年前

    下面是执行数据绑定的方法。您可以确切地看到发生了什么:

    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;
        }
    }
    
        3
  •  3
  •   Serhat Ozgel    16 年前

    如果您正在构建ListItems,那么首先就不需要使用DataBind()。

    只需将它们添加到您的下拉列表:

    
    
    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"));
    
    

    通过设置DataTextField和DataValueField(如buyutec所写),当您已经有一个可以用作数据源的集合/数据对象(通常是DataTable或DataView)时,DataBind()非常有用。

        4
  •  1
  •   John Doe John Doe    15 年前

    直接添加到dropdownlist是一种简单的方法(并且给出了正确的示例代码),但是假设您有一个无序的数据源,并且希望列表项排序。

    实现这一点的一种方法是创建ListItem的通用列表,然后在数据绑定到列表之前使用继承的排序方法。