Asp.net

將 ListItems 從通用列表添加到 DropDownList

  • June 3, 2009

我有一個這個 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"))方法它可以工作,但出於其他原因我需要使用通用列表作為數據源。

因為 DataBind 方法僅在設置了 DataValueField 屬性時才綁定值。如果在呼叫 DataBind 之前將 DataValueField 屬性設置為“Value”,則您的值將出現在標記上。

更新:您還需要將 DataTextField 屬性設置為“文本”。這是因為數據綁定和手動添加項目的工作方式不同。數據綁定不知道 ListItem 類型的存在,並通過評估數據源中的項目來生成標記。

這是執行數據綁定的方法。您可以準確地看到發生了什麼:

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

引用自:https://stackoverflow.com/questions/132643