Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8212239
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T10:41:16+00:00 2026-06-07T10:41:16+00:00

I have this unexpected behaviour with an ASP.NET web forms DropDownList. I am adding

  • 0

I have this unexpected behaviour with an ASP.NET web forms DropDownList. I am adding ListItems to a drop down list, and all looks well. Except that the value in the ddl (which in my case is the Id) is never set, it is replaced by the text from the ListItem.

<asp:DropDownList ID="ddl1" runat="server">
        </asp:DropDownList>


    private void Populate()
    {
        List<ListItem> list = new List<ListItem>();
        foreach (var item in GetItems())
        {
            list.Add(new ListItem(item.name, item.id.ToString()));
            //in the drop down list the ListItem.Value is being replaced
            //by ListItem.Text when it is added to the ddl
        }

        ddl1.DataSource = list;
        ddl1.DataBind();

        ddl1.Items.Add(new ListItem("Make a selection"));
    }

    private List<Stuff> GetItems()
    {
        List<Stuff> list = new List<Stuff>();
        list.Add(new Stuff{ name = "bill", id = 1});
        list.Add(new Stuff{ name = "james", id = 2});
        return list;
    }

    private struct Stuff
    {
        public string name;
        public int id;
    }

Any ideas if this is what is meant to happen? And if it is how do I store both a name and an Id in the ddl?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-07T10:41:19+00:00Added an answer on June 7, 2026 at 10:41 am

    In your populate method, you are data binding, but not specifying which property should bind to which value in your list of list items.

    To do this you need to set the DataTextField and DataValueField properties.

    You might find it simpler to change your Populate code to the following:

    private void Populate() 
    { 
    
        foreach (var item in GetItems()) 
        { 
            ddl1.Items.Add(new ListItem(item.name, item.id.ToString())); 
        } 
    
    
        ddl1.Items.Insert(0, new ListItem("Make a selection")); 
    } 
    

    Or even:

    private void Populate() 
    { 
        GetItems().ForEach(x => dd1.Items.Add(new ListItem(x.name, x.id.ToString())));
        ddl1.Items.Insert(0, new ListItem("Make a selection")); 
    } 
    

    i.e. Just add your items to the DropDownList directly.

    In your original example, to use databinding, there’s no need to create a List of ListItem. You can databind stuff directly; in this instance, your code becomes:

    private void Populate() 
    { 
        dd1.DataTextField = "name";
        dd1.DataValueField = "id"
        dd1.DataSource = GetItems();
        ddl1.Items.Insert(0, new ListItem("Make a selection")); 
    }
    

    i.e. you can databind direct to a list of stuff as long as you tell the DropDownList where to get it’s values from. Many people prefer this model as the SelectedItem is then a Stuff, rather than a string, so you have the object representing the item directly. The disadvantage is that your ViewState just got bigger 😉

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have an ASP.NET 3.5 web site project. In it there exists a page
I have successfully setup up mocking to test asp.net forms authorization but I am
I am getting some unexpected behavior from Html.EditorFor(). I have this controller: [HandleError] public
I have an unexpected ; error on this line: $action = ($sessionMinus == $_SESSION['initial_count'])
Have this self-made slider: http://jsfiddle.net/wyc3P/4/ What it does: takes min and max values in
I have noticed some unexpected behaviour when using the jQuery .ready() function, whereby afterwards
I'm using VS2010,C# to develop my ASP.NET web app, sometimes I need to declare
I'm seeing a behaviour in Firefox which seems to me unexpected. This is not
I have the following problem in ASP.NET: there is a form that contains a
If I have something like this (pseudocode): class A { List<SomeClass> list; private void

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.