473,785 Members | 2,641 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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:dropdownLis t.
Does anyone have any sample code to do this? Like a foreach loop.

Thanks for any help,
CK
May 17 '06 #1
8 1867
hi

foreach(string s in Enum.GetNames( typeof( your_enum) )
dropdownlist1.I tems.Add( s);
--
--
Ignacio Machin,
ignacio.machin AT dot.state.fl.us
Florida Department Of Transportation
"CK" <c_**********@h otmail.com> wrote in message
news:h3******** ***********@new ssvr11.news.pro digy.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:dropdownLis t. 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.co m

"CK" <c_**********@h otmail.com> wrote in message
news:h3******** ***********@new ssvr11.news.pro digy.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:dropdownLis t. 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.mach in AT dot.state.fl.us > wrote
in message news:%2******** ********@TK2MSF TNGP03.phx.gbl. ..
hi

foreach(string s in Enum.GetNames( typeof( your_enum) )
dropdownlist1.I tems.Add( s);
--
--
Ignacio Machin,
ignacio.machin AT dot.state.fl.us
Florida Department Of Transportation
"CK" <c_**********@h otmail.com> wrote in message
news:h3******** ***********@new ssvr11.news.pro digy.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:dropdownLis t. 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.ListVal ues(). 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.co m

"CK" <c_**********@h otmail.com> wrote in message
news:h3******** ***********@new ssvr11.news.pro digy.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:dropdownLis t. 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 relatedExperien ce_DataBinding( object sender, EventArgs e)

{

DropDownList relatedExperien ce = (DropDownList) sender;

foreach(Experie nceRelated er in Enum.GetValues( typeof(Experien ceRelated)))

{

relatedExperien ce.Items.Add(ne w ListItem(er.ToS tring(),er.ToSt ring("d")));

}

relatedExperien ce.Items.Insert (0,"");

}
"Nate" <Na**@discussio ns.microsoft.co m> wrote in message
news:86******** *************** ***********@mic rosoft.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.ListVal ues(). 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.co m

"CK" <c_**********@h otmail.com> wrote in message
news:h3******** ***********@new ssvr11.news.pro digy.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:dropdownLis t. 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_**********@h otmail.com> wrote in message
news:9X******** ***********@new ssvr29.news.pro digy.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**@discussio ns.microsoft.co m> wrote in message
news:86******** *************** ***********@mic rosoft.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
1257
by: tony collier | last post by:
I have the following in one of my .aspx pages: ..... <script runat="server"> enum bookstores { Amazon,
3
2006
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
1461
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
5472
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 loop through the filtered GridView to identify the selected rows and assemble some XML to be sent to a stored proc. The problem I have is that when looping through the GridView, it doesn't
7
4726
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
1994
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 do this? Like a foreach loop. Thanks for any help, CK
8
3938
by: SpecialKay | last post by:
Sorry if this is not the place for C#. in short: public enum MyEnum { One, Two, Three };
10
6136
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
1116
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, but it doesn't work and I don't know the correct way to do this. Is this possible? If so, what should each of my foreach loops look like? Thanks. private enum Colors { Red, Yellow, Blue, Green }; private enum Days { Sun, Mon, Tue, Wed, Thu,...
0
9645
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9480
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10325
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
10091
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8972
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7499
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
1
4053
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3646
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2879
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.