473,395 Members | 1,454 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,395 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 1963
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,
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...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
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,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
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...

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.