473,769 Members | 6,799 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 1993
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,
8
1867
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
0
10223
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
10000
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
9866
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8879
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
7413
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...
0
5310
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5448
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3968
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
3571
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.