Domanda

Esiste comunque un elemento in un DropDownList ASP.NET che abbia il proprio Testo o Valore associato a un metodo sull'origine anziché a una proprietà?

È stato utile?

Soluzione

L'unico modo per farlo è gestire l'evento Databinding di DropDownList, chiamare il metodo e impostare personalmente i valori nell'elemento DropDownList.

Altri suggerimenti

Questa è la mia soluzione:

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

Ecco 2 esempi per associare un menu a discesa in ASP.net da una classe

La tua pagina aspx

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

La tua pagina 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();     
    }

La tua classe 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;
    }
}

A volte ho bisogno di usare le proprietà di navigazione come DataTextField, come (" User.Address.Description "), quindi ho deciso di creare un controllo semplice che deriva da DropDownList. Ho anche implementato un evento ItemDataBound che può aiutare anche.

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

dichiarativo:

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

Quanto sopra si lega a un metodo che restituisce un elenco generico, ma è anche possibile associare un metodo che restituisce un DataReader. È inoltre possibile creare l'origine dati nel codice.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top