.NET Tutorials, Forums, Interview Questions And Answers
Welcome :Guest
Sign In
Win Surprise Gifts!!!

Top 5 Contributors of the Month
Gaurav Pal
Post New Web Links

Populating a DropDown ListBox from an Enum

Posted By:      Posted Date: October 08, 2010    Points: 0   Category :ASP.Net

I have this simple Enum defined:

    public enum Grade : byte
      Up = 1,
      Down = 2,
      Flat = 4

I've tried using it to populate a DropDown ListBox like this:

    ddList.DataSource = Enum.GetValues(typeof(Constants.Grade));

The problem is that BOTH the Text and the Value of each ListItem ends up with the text string.  What I want, of course, is for each ListItem's Value property to be populated with the numeric value.

Is there a simple way to do this?


View Complete Post

More Related Resource Links

Populating Dropdown List using FK


Hi Folks,

2 Questions. I am trying out Data Entities in VWD 2010 Express. I have a parent (Customers) and child (Orders) table. Customers table has FirstName, LastName and Email columns.

Q1. When I view/edit/insert an Order, it shows the customer's first name in dropdown list whereas I want the full name. How can this be done?

Q2. How can I change the default behavior of PK/FK relationship so that customer's Email is displayed instead of Name?

Any help will be much appreciated.


Populating 3 dropdown each other.

Hi all, I have a complicate problem.  I'm not sure if I can explain it clearly.I have SQL table with columns:  id, PartNo, ManuPartNo, GovtPartNum.My boss want to do like this with 3 dropdown or 1 Dropdown with 2 listbox:  If he select any PartNo, it will pull associated value For ManuPartNo & GovtPartNumAlso, if he select any ManuPartNo, it will populate back to other dropdownAlso, if he select any GovtPartNum, it will populate back to other dropdownI'm so stuck & I don't know where to start on this.  Can anyone help?Thanks

Dynamically (on client side) populating data control (repeater) when dropdown item is selected

Hello!I'm currently working on a project and I want to make as much things as possible performing on client side - with as little loading as possible.The project consist of three "main" pages. On one page I use jQuery DataTable plugin which is working great. One page has tabs (using jQuery). I want to put datacontrol (repeater) on this site and when  user selects one item in dropdown, data in repeater loads on client side, without performing the action on server.I wonder if this is possible and if somebody can give me some case to help with?Thanks!I hope I was clear enough. all the best

populating listbox very very slow

in asp.net 2.0 in my application i have a listbox & checkboxlistboth of them in update panel tp prevent post back listbox is poproplated with data & depeneding on the item selected in list box checklistbox gets populatedthe thigs r working perfect except that when the change of item in listbox takes place checklistbox is very very slow to get populated it takes a lot of time

Populating dropdown based on checkbox value in Infopath

Hi I have created a form library with the following values ID  Document   Preference 1   A                Yes 2   B                No 3   C                Yes   I have a drop-down and a checkbox in an infopath form. This dropdown will by default display the ID numbers. When a user checks the checkbox, then the dropdown should display only the id's that has "Preference" value equal to "Yes". I am using Infopath 2010 How to do this...Any suggestions...

Populating a dropdown box with contents based on the selection of another dropdown box

Hi All   I am a complete newcomer to ASP.NET.  However in the few weeks I have been playing with it I have got pretty far and  have mastered things like stored procedures, gridviews, formviews and templates.  I have come across a problem which I am struggling with and could do with some help!  The scenario is this:   I have a dropdown box which is populated from SQLDataSource1   I have a second dropdown box which is populated from SQLDataSource2, but i need the query feeding this to be dependent on what was selected in the first dropdown box.    I assumed it would simply be a case of modifying the select statement on SQLDataSource2 using the SelectedIndexChanged event on the first DropDownList to update the second SQL source, but I cannot work out for the life of me how to do it.  Pretty much all of the code I have is ASP.   Is there a way I can do this purely in ASP rather than using VB behind the scenes or do I need to use VB?  I am pretty comfortable with vb.net but one of the other issues I have is referencing an ASP object from VB... so for instance if I had a text box in ASP, how can I read or change the properties of this object from VB?   FYI I am using Visual Studio 2010 and SQL/Server   Any help much appreciated!!!   Sean   

Coloring DropDown ListBox Items


I have a situation where some dropdown listboxes contain some items, then there's a separator line, and then there are more items.  The ones above the separator line reflect the "commonly used items".  To better highlight these I was using code like this to individually color those specfic items:

         foreach (ListItem item in ddList.Items)
          if (item.Value == "-1")
            unAssigned = true;

          if (!unAssigned)
            item.Attributes.Add("style", "color:#5558ff");

This code works fine EXCEPT that once a PostBack occurs then the highlighting disappears.  The items in the dropdown listbox remain (without repopulation) but not the highlighting.

I'm curious why this is happening and whether there's a way for the highlighting to "stick" short of calling this code from the Page_Load event handler every Postback?


Problem with populating dropdown in code behind


Hi all, I'm having problems with this code not adding the list items to my dropdown. I was hoping someone could point out why.


On my aspx page I have:

<asp:DropDownList id="AS400_wh_c1_act" runat="server"></asp:DropDownList>

<% PopDrop(AS400_wh_c1_act)%>

In my code behind, I have:

    Public Sub PopDrop(ByVal input As DropDownList)
        input.Items.Add(New ListItem("", ""))
        input.Items.Add(New ListItem("PAGER", "PAGER"))
        input.Items.Add(New ListItem("EMAIL", "EMAIL"))
        input.Items.Add(New ListItem("STANDBY", "STANDBY"))
    End Sub

However, the dropdown list isn't adding the new items.

Anone know what I'm doing wrong?


How to make a DropDown ListBox non-centered?


Hello, I put a DropDown ListBox on a Form.  It is centered in a td.style control.  Basically, I am trying to figure out how to make it non-centered.  I'd like to place it in a specific spot and have it stay there, but I can't seem to un-center it.  I've done this before, a while back.  Now, I forget how to do it.  Below is my code:

<td>&nbsp;<b><span><a href="Login.aspx"
        <span><asp:DropDownList ID="DropDownList1" runat="server"
            AutoPostBack="True" Width="162px">
        <br />
        <br />
            <span> <a href="Login.aspx"
        <asp:DropDownList ID="DropDownList2" runat="server">

Populating a ListBox with values from an object ??????


I have a myClass with 2 fields:


private Int16 samAdresse = -1;

private commonThings.enTilstand skiftTilTilstand = commonThings.enTilstand.ingenMening;

Instances of this class I store in a List<myClass> myList

Grab text from dropdown listbox?


Every time I chage the listbox I want to text in the listbox to display on a seprate label on my form. I have used this:

lblTreatment.Text = ddlTreatment.SelectedItem.Text

in the selectedIndexChanged and it works, but I want to select the text when the page first loads up. When I put it in the page_Load I get this error:

Object reference not set to an instance of an object.

And this error is for the code above. Any help?

Thank You

what is the syntax for populating dropdown in Javascript using web service?



I have written the web method in web service like this


    public List<Location> FindAllCountries()
        List<Location> locations = new List<Location>();
        string connectionString =
            "Data Source=.;Initial Catalog=Test;Integrated Security=True";
        using (SqlConnection connection = new SqlConnection(connectionString))
            using (SqlCommand command = new SqlCommand())
                command.Connection = connection;
                command.CommandText = "SelectAllCountries";
                command.CommandType = CommandType.StoredProcedure;
                using (SqlDataReader dataReader = command.ExecuteReader())
                    Location location;
                    if (dataReader != null)
                        while (dataReader.Read())
                            location = new Location();
                            location.CountryID = (int)dataReader["CountryID"];
                            location.CountryName = Convert.ToString(dataReader["CountryName"]);
        return locations;

I m calling the webservic

ListBox tricky to set in a DataGrid/GridView

Listboxes are very tricky to set selected items in grid - there is no real nice way to do it. It would have been great if Microsoft included a property that accepts an array or comma separated values - or anything! But they only allow each item in the list to be selected individually. The way to do this in a grid is a bit messy, but it is fairly simple to implement. When a row is created (as per the event), select the items of the listbox as per the datagrid source data:

Working with DropDownList and ListBox Controls in ASP.NET

"In my application i am using dropdownlist to display all categories from a table. When the user clicks this control I display all records in that category. Category table also contains column called color.

I wanted display some categories in dropdown list different color and others in different color using ASP.NET."-Anonymous

Dropdown list in ASP.NET - How to get value selected

am sure this must be very simple, but unforunately I can't figure it out.
I have the following code which populates the dropdown lists from the database. This works fine. Now, when the user selects a particular employee name from the list, I want to get the Employee Id of the selected employee. i.e. I need to get the "Eid" value. I can do this if the EId is loaded in the dropdown list, but in this case only the employee name is displayed. Please let me know howI can get the Eid value even though it is not loaded in the dropdown list.
I would really appreciate your help

Editable Gridview with Textbox, CheckBox, Radio Button and DropDown List

This is an example of an editable gridview containing different ASP.NET controls. When user wants to edit values in gridview, she/he can use these controls to edit existing values in Gridview Edit Mode.

Using the Code

Binding an array to dropdown list


I have the following string which is a list of urls. How do I get this to work so the url shows for the selection and also is the value?      


allLinks = newAllLinks
        Me.DropDownList1.DataTextField = "Link"
        Me.DropDownList1.DataValueField = "LinkURL"
        Me.DropDownList1.Items.Insert(0, New ListItem(" - Select - ", 0))
        Me.DropDownList1.DataSource = allLinks


 ArgumentNullException: Value cannot be null.
Parameter name: container]
   System.Web.UI.DataBinder.GetPropertyValue(Object container, String propName) +121
   System.Web.UI.DataBinder.GetPropertyValue(Object container, String propName, String format) +8

ASP.NetWindows Application  .NET Framework  C#  VB.Net  ADO.Net  
Sql Server  SharePoint  Silverlight  Others  All   

Hall of Fame    Twitter   Terms of Service    Privacy Policy    Contact Us    Archives   Tell A Friend