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 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
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
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
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
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 >
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
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 ;)
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 This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
by: tony collier |
last post by:
I have the following in one of my .aspx pages:
.....
<script runat="server">
enum bookstores
{
Amazon,
|
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
|
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...
|
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,...
|
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...
| |
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...
|
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...
|
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...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
|
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
|
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |