473,288 Members | 2,350 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,288 software developers and data experts.

Looping through Enum on Page_Load

CK
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
May 17 '06 #1
8 1846
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_**********@hotmail.com> wrote in message
news:h3*******************@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

May 17 '06 #2
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]
- mv*@spam.guard.caspershouse.com

"CK" <c_**********@hotmail.com> wrote in message
news:h3*******************@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

May 17 '06 #3
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

"Ignacio Machin ( .NET/ C# MVP )" <ignacio.machin AT dot.state.fl.us> wrote
in message news:%2****************@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_**********@hotmail.com> wrote in message
news:h3*******************@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


May 17 '06 #4
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]
- mv*@spam.guard.caspershouse.com

"CK" <c_**********@hotmail.com> wrote in message
news:h3*******************@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


May 17 '06 #5
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,"");

}
"Nate" <Na**@discussions.microsoft.com> wrote in message
news:86**********************************@microsof t.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]
- mv*@spam.guard.caspershouse.com

"CK" <c_**********@hotmail.com> wrote in message
news:h3*******************@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
>


May 17 '06 #6
Nate wrote:
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..

I would challenge that "bad design" statement.
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
May 18 '06 #7
Hi,
"CK" <c_**********@hotmail.com> wrote in message
news:9X*******************@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

Enum.GetValues ?

C'mon a little search in msdn would had give you that ;)
May 18 '06 #8
Hi,

"Nate" <Na**@discussions.microsoft.com> wrote in message
news:86**********************************@microsof t.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..


//inverted just in case :)
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
May 18 '06 #9

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: tony collier | last post by:
I have the following in one of my .aspx pages: ..... <script runat="server"> enum bookstores { Amazon,
3
by: M | last post by:
I can pass in a string to a Web User Control, but I haven't been successful passing in an enum. I declared my enum as a public variable in my Web User Control itself. Thanks in advance.
2
by: wapsiii | last post by:
I'm trying to do this http://geekswithblogs.net/jawad/archive/2005/06/24/EnumDropDown.aspx public enum Color { RED, GREEN, BLUE }
7
by: Ken | last post by:
Hi All - I have a filtered GridView. This GridView has a check box in the first column. This check box is used to identify specific rows for delete operations. On the button click event I...
7
by: Wolf | last post by:
Hi all Who can Help me with this one? if I have this Enums: #region Title public enum Title: short { NA = 0,
8
by: CK | last post by:
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...
8
by: SpecialKay | last post by:
Sorry if this is not the place for C#. in short: public enum MyEnum { One, Two, Three };
10
by: Doug | last post by:
I have an enum that will look kind of like this: private enum MyEnum { SomeValue = 0, AnotherValue = 1, ThirdValue = 2 }
1
by: =?Utf-8?B?RGF2aWQgUg==?= | last post by:
I'd like to create a list of enumerated types, and then loop through each of those types to perform a standard operation on each. The following sample gives an idea of what I'd like to accomplish,...
0
by: MeoLessi9 | last post by:
I have VirtualBox installed on Windows 11 and now I would like to install Kali on a virtual machine. However, on the official website, I see two options: "Installer images" and "Virtual machines"....
0
by: Aftab Ahmad | last post by:
Hello Experts! I have written a code in MS Access for a cmd called "WhatsApp Message" to open WhatsApp using that very code but the problem is that it gives a popup message everytime I clicked on...
0
by: Aftab Ahmad | last post by:
So, I have written a code for a cmd called "Send WhatsApp Message" to open and send WhatsApp messaage. The code is given below. Dim IE As Object Set IE =...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: marcoviolo | last post by:
Dear all, I would like to implement on my worksheet an vlookup dynamic , that consider a change of pivot excel via win32com, from an external excel (without open it) and save the new file into a...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.