2008-09-25 187 views

回答

0

要做到這一點的唯一方法是處理DropDownList的Databinding事件,調用方法並自己設置DropDownList項中的值。

-4

以聲明:

<asp:DropDownList ID="ddlType" runat="server" Width="250px" AppendDataBoundItems="true" DataSourceID="dsTypeList" DataTextField="Description" DataValueField="ID"> 
    <asp:ListItem Value="0">All Categories</asp:ListItem> 
</asp:DropDownList><br /> 
<asp:ObjectDataSource ID="dsTypeList" runat="server" DataObjectTypeName="MyType" SelectMethod="GetList" TypeName="MyTypeManager"> 
</asp:ObjectDataSource> 

以上結合到返回泛型列表的方法,但你也可以綁定到返回一個DataReader的方法。你也可以用代碼創建你的數據源。

+1

在您的示例中,DataTextField和DataValueField是屬性。我需要將源代碼中調用方法的結果作爲文本或值。 – kenstone 2008-09-25 18:04:32

3

這是我的解決方案:

<asp:DropDownList ID="dropDownList" runat="server" DataSourceID="dataSource" DataValueField="DataValueField" DataTextField="DataTextField" /> 
<asp:ObjectDataSource ID="dataSource" runat="server" SelectMethod="SelectForDataSource" TypeName="CategoryDao" /> 

public IEnumerable<object> SelectForDataSource() 
{ 
    return _repository.Search().Select(x => new{ 
     DataValueField = x.CategoryId, 
     DataTextField = x.ToString() // Here is the trick! 
    }).Cast<object>(); 
} 
0

有時候,我需要使用導航屬性爲DataTextField,像(「User.Address.Description」),所以我決定創建一個簡單的控制,從DropDownList中派生。 我也實現了一個ItemDataBound事件,可以幫助。

public class RTIDropDownList : DropDownList 
{ 
    public delegate void ItemDataBoundDelegate(ListItem item, object dataRow); 
    [Description("ItemDataBound Event")] 
    public event ItemDataBoundDelegate ItemDataBound; 

    protected override void PerformDataBinding(IEnumerable dataSource) 
    { 
     if (dataSource != null) 
     { 
      if (!AppendDataBoundItems) 
       this.Items.Clear(); 

      IEnumerator e = dataSource.GetEnumerator(); 

      while (e.MoveNext()) 
      { 
       object row = e.Current; 

       var item = new ListItem(DataBinder.Eval(row, DataTextField, DataTextFormatString).ToString(), DataBinder.Eval(row, DataValueField).ToString()); 

       this.Items.Add(item); 

       if (ItemDataBound != null) // 
        ItemDataBound(item, row); 
      } 
     } 
    } 
} 
1

這裏有兩個例子從一個類

你的aspx頁面

<asp:DropDownList ID="DropDownListJour1" runat="server"> 
    </asp:DropDownList> 
    <br /> 
    <asp:DropDownList ID="DropDownListJour2" runat="server"> 
    </asp:DropDownList> 

您的aspx.cs頁面

protected void Page_Load(object sender, EventArgs e) 
    { 
    //Exemple with value different same as text (dropdown) 
    DropDownListJour1.DataSource = jour.ListSameValueText();    
    DropDownListJour1.DataBind(); 

    //Exemple with value different of text (dropdown) 
    DropDownListJour2.DataSource = jour.ListDifferentValueText(); 
    DropDownListJour2.DataValueField = "Key"; 
    DropDownListJour2.DataTextField = "Value"; 
    DropDownListJour2.DataBind();  
    } 

你怨婦結合在ASP.net下拉。 cs class(jour.cs)

public class jour 
{ 

    public static string[] ListSameValueText() 
    { 
     string[] myarray = {"a","b","c","d","e"} ; 
     return myarray; 
    } 

    public static Dictionary<int, string> ListDifferentValueText() 
    { 
     var joursem2 = new Dictionary<int, string>(); 
     joursem2.Add(1, "Lundi"); 
     joursem2.Add(2, "Mardi"); 
     joursem2.Add(3, "Mercredi"); 
     joursem2.Add(4, "Jeudi"); 
     joursem2.Add(5, "Vendredi"); 
     return joursem2; 
    } 
} 
+0

非常有用,我能找到的唯一答案之一顯示瞭如何設置DataTextField和DataValueField – 2016-06-30 07:35:13

相關問題