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

这是 2 个示例,用于从类中绑定 ASP.net 中的下拉列表

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

唯一的方法是处理 DropDownList 的 Databinding 事件,调用该方法并自己设置 DropDownList 项中的值。

于 2008-09-25T17:35:59.103 回答
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 );
            }
        }
    }
}
于 2011-12-16T01:25:18.047 回答
-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 的方法。您还可以在代码中创建数据源。

于 2008-09-25T17:58:59.407 回答