Come fare il bind di una DropDownList da un enumerator

Author: ollie10 9/15/2008 8:48 PM

Spesso capita (maggari in un cms) di dover fare il databind di una DropDownList partendo da un enumerator, inserendo nel testo il nome del valore e nel campo value il valore numerico effettivo. Con il codice seguente si può fare velocemente e senza dolori...

string[] enums = Enum.GetNames(typeof(MyEnum));
ListItem listItem;
for (int i = 0; i < enums.Length - 1; i++)
{
	listItem = new ListItem(enums[i], Convert.ToInt32(Enum.Parse(typeof(MyEnum), enums[i])).ToString());
	myDropDownList.Items.Add(listItem);
}
myDropDownList.DataBind();
Tags: , ,
Categories: Asp.NET - C#
Permalink | Comments (2)
  • condividi Come fare il bind di una DropDownList da un enumerator su facebook
  • condividi Come fare il bind di una DropDownList da un enumerator su twitter
  • condividi Come fare il bind di una DropDownList da un enumerator su stumbleupon
  • condividi Come fare il bind di una DropDownList da un enumerator su digg
  • condividi Come fare il bind di una DropDownList da un enumerator su delicious
  • condividi Come fare il bind di una DropDownList da un enumerator su reddit
  • condividi Come fare il bind di una DropDownList da un enumerator su google
  • condividi Come fare il bind di una DropDownList da un enumerator su netvibes
  • aggiungi il feed di Come fare il bind di una DropDownList da un enumerator

Comments (2) -


Italy imperugo
4/8/2009 11:31 PM
Così non fai il fatabind, aggiungi elementi alla collection di Items.
Il risultato è lo stesso ma è un approccio differente.
Il databind lo potevi fare se associavi un datasource alla DropDownList.
Con il tuo approccio puoi anche non invocare il metodo DataBind.

Ciauz
;)


 ollie10 (website)
4/9/2009 11:36 AM
Hai ragione la riga myDropDownList.DataBind(); è di troppo

Add comment




  Country flag
biuquote
  • Comment
  • Preview
Loading