|
.net
newsgroups
|
|||||||||||||||||||||||
|
|||||||||||||||||||||||
Looping through Enum on Page_LoadHi All,
Good morning. I had a quick question. I have a public Enum. During page load I want to loop thru the Enum and put those as items in an asp:dropdownList. Does anyone have any sample code to do this? Like a foreach loop. Thanks for any help, CK hi
foreach(string s in Enum.GetNames( typeof( your_enum) ) dropdownlist1.Items.Add( s); -- Show quoteHide quote-- Ignacio Machin, ignacio.machin AT dot.state.fl.us Florida Department Of Transportation "CK" <c_kettenb***@hotmail.com> wrote in message news:h3Iag.28486$4L1.26570@newssvr11.news.prodigy.com... > Hi All, > Good morning. I had a quick question. I have a public Enum. During page > load I want to loop thru the Enum and put those as items in an > asp:dropdownList. Does anyone have any sample code to do this? Like a > foreach loop. > > Thanks for any help, > CK > Thanks Ignacio, this worked great, but how do i get the Enum values to be
the vlaues of the list items? Thanks again ~CK Show quoteHide quote "Ignacio Machin ( .NET/ C# MVP )" <ignacio.machin AT dot.state.fl.us> wrote in message news:%23cyUH7ceGHA.4720@TK2MSFTNGP03.phx.gbl... > hi > > foreach(string s in Enum.GetNames( typeof( your_enum) ) > dropdownlist1.Items.Add( s); > > > -- > -- > Ignacio Machin, > ignacio.machin AT dot.state.fl.us > Florida Department Of Transportation > > > "CK" <c_kettenb***@hotmail.com> wrote in message > news:h3Iag.28486$4L1.26570@newssvr11.news.prodigy.com... >> Hi All, >> Good morning. I had a quick question. I have a public Enum. During page >> load I want to loop thru the Enum and put those as items in an >> asp:dropdownList. Does anyone have any sample code to do this? Like a >> foreach loop. >> >> Thanks for any help, >> CK >> > > Hi,
"CK" <c_kettenb***@hotmail.com> wrote in message Enum.GetValues ?news:9XIag.73068$F_3.38251@newssvr29.news.prodigy.net... > Thanks Ignacio, this worked great, but how do i get the Enum values to be > the vlaues of the list items? Thanks again ~CK C'mon a little search in msdn would had give you that ;) CK,
You should be able to call the static GetNames method on the Enum class (passing the type of your enumeration), and then bind to the array of strings that is returned to you. Also, you might want to cache the string array somewhere, if you are going to populate this list often. After all, the enumeration (and therefore the array) is not going to change while the web site is up. Hope this helps. -- Show quoteHide quote- Nicholas Paldino [.NET/C# MVP] - mvp@spam.guard.caspershouse.com "CK" <c_kettenb***@hotmail.com> wrote in message news:h3Iag.28486$4L1.26570@newssvr11.news.prodigy.com... > Hi All, > Good morning. I had a quick question. I have a public Enum. During page > load I want to loop thru the Enum and put those as items in an > asp:dropdownList. Does anyone have any sample code to do this? Like a > foreach loop. > > Thanks for any help, > CK > This sounds like a bad design approach to even use enums as a basis for
databinding. An easier to code and scalable approach would be to create a seperate class that provides a method which returns a DataTable which could then be bound to.. public class MyClass { public static DataTable ListValues() { DataTable dt = new DataTable(); // create columns and add rows to datatable return dt; } } This will be beneficial for future development as well if you decide to move the values into a database. The only thing that would need to be changed would be the innerworkings of MyClass.ListValues(). And... this is much easier to DataBind to. Show quoteHide quote "Nicholas Paldino [.NET/C# MVP]" wrote: > CK, > > You should be able to call the static GetNames method on the Enum class > (passing the type of your enumeration), and then bind to the array of > strings that is returned to you. > > Also, you might want to cache the string array somewhere, if you are > going to populate this list often. After all, the enumeration (and > therefore the array) is not going to change while the web site is up. > > Hope this helps. > > > -- > - Nicholas Paldino [.NET/C# MVP] > - mvp@spam.guard.caspershouse.com > > "CK" <c_kettenb***@hotmail.com> wrote in message > news:h3Iag.28486$4L1.26570@newssvr11.news.prodigy.com... > > Hi All, > > Good morning. I had a quick question. I have a public Enum. During page > > load I want to loop thru the Enum and put those as items in an > > asp:dropdownList. Does anyone have any sample code to do this? Like a > > foreach loop. > > > > Thanks for any help, > > CK > > > > > I went with this code and it works pretty well
private void relatedExperience_DataBinding(object sender, EventArgs e) { DropDownList relatedExperience = (DropDownList) sender; foreach(ExperienceRelated er in Enum.GetValues(typeof(ExperienceRelated))) { relatedExperience.Items.Add(new ListItem(er.ToString(),er.ToString("d"))); } relatedExperience.Items.Insert(0,""); } Show quoteHide quote "Nate" <N***@discussions.microsoft.com> wrote in message news:861C285C-5718-4DCA-8CCD-3DCB8701DC7F@microsoft.com... > This sounds like a bad design approach to even use enums as a basis for > databinding. An easier to code and scalable approach would be to create a > seperate class that provides a method which returns a DataTable which > could > then be bound to.. > > public class MyClass > { > public static DataTable ListValues() > { > DataTable dt = new DataTable(); > // create columns and add rows to datatable > return dt; > } > } > > This will be beneficial for future development as well if you decide to > move > the values into a database. The only thing that would need to be changed > would be the innerworkings of MyClass.ListValues(). And... this is much > easier to DataBind to. > > "Nicholas Paldino [.NET/C# MVP]" wrote: > >> CK, >> >> You should be able to call the static GetNames method on the Enum >> class >> (passing the type of your enumeration), and then bind to the array of >> strings that is returned to you. >> >> Also, you might want to cache the string array somewhere, if you are >> going to populate this list often. After all, the enumeration (and >> therefore the array) is not going to change while the web site is up. >> >> Hope this helps. >> >> >> -- >> - Nicholas Paldino [.NET/C# MVP] >> - mvp@spam.guard.caspershouse.com >> >> "CK" <c_kettenb***@hotmail.com> wrote in message >> news:h3Iag.28486$4L1.26570@newssvr11.news.prodigy.com... >> > Hi All, >> > Good morning. I had a quick question. I have a public Enum. During page >> > load I want to loop thru the Enum and put those as items in an >> > asp:dropdownList. Does anyone have any sample code to do this? Like a >> > foreach loop. >> > >> > Thanks for any help, >> > CK >> > >> >> >> Nate wrote:
> This sounds like a bad design approach to even use enums as a basis for I would challenge that "bad design" statement.> databinding. An easier to code and scalable approach would be to create a > seperate class that provides a method which returns a DataTable which could > then be bound to.. > It would also be quite unlikely that it would change in a lot (majority) of cases so you would invest the extra effort of doing the premature architectural optimisations that would quite likely never be needed. It also of course adds complexity and additional points of failure. The Simplest Thing That Could Possibly Work is a good maxim imo. You can also bind directly to Enum.GetNames(.... <...> JB Hi,
"Nate" <N***@discussions.microsoft.com> wrote in message //inverted just in case :)news:861C285C-5718-4DCA-8CCD-3DCB8701DC7F@microsoft.com... > This sounds like a bad design approach to even use enums as a basis for > databinding. An easier to code and scalable approach would be to create a > seperate class that provides a method which returns a DataTable which > could > then be bound to.. public enum Xes { Male, Female }; I strongly doubt this enum will change anytime soon :) -- -- Ignacio Machin, ignacio.machin AT dot.state.fl.us Florida Department Of Transportation
Other interesting topics
ActiveDirectoryMembershipProvider
"Transparently" printing a word document GridView: conditional editing FindControl in GridView ValidateRequest customization Importing CSS file in page that uses a master page Same function different signatures FormView and FileUpload JavaScript not working. Profiles |
|||||||||||||||||||||||