6

とにかく、ASP.NET DropDownList のアイテムに、プロパティではなくソースのメソッドにバインドされた Text または Value を持たせる方法はありますか?

4

5 に答える 5

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>();
}
于 2009-10-20T09:52:17.743 に答える
1

クラスから ASP.net のドロップダウンをバインドするための 2 つの例を次に示します。

あなたの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();     
    }

jour.cs クラス (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;
    }
}
于 2013-02-23T01:12:43.507 に答える
0

("User.Address.Description")のように、ナビゲーションプロパティをDataTextFieldとして使用する必要がある場合があるため、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 );
            }
        }
    }
}
于 2011-12-16T01:25:18.047 に答える
0

これを行う唯一の方法は、DropDownList の Databinding イベントを処理し、メソッドを呼び出して、DropDownList 項目の値を自分で設定することです。

于 2008-09-25T17:35:59.103 に答える
-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を返すメソッドにバインドすることもできます。コードでdataSourceを作成することもできます。

于 2008-09-25T17:58:59.407 に答える