Click here to Skip to main content
13,247,494 members (64,399 online)
Rate this:
Please Sign up or sign in to vote.
See more:
I am trying to add an attribute to a dropdownlist control to store a value returned for the database. This value is need to get a code once a selection is made on the dropdownlist;

I have tried the following:

ddlStatus.DataTextField = dsValues.Tables(0).Columns("Description").ToString ()
ddlStatus.DataValueField = dsValues.Tables(0).Columns("CategoryCode").ToStrin g()
ddlStatus.Attributes.Add("SysCode", dsValues.Tables(0).Columns("SystemCode").ToString( ))

but the attribute is not bound to the control
Posted 18-Aug-10 15:04pm
Updated 28-Jan-17 10:39am
Rate this: bad
Please Sign up or sign in to vote.

Solution 1

I created the following codes and commented inline so that you can understand how to bind data to a DropDownList and how to add an attribute to each item in the drop down list (Assuming that, you wanted to add an attribute to each item).

Here is the Drop-Down list in the aspx page:

<asp:DropDownList ID="ddlStatus" runat="server">

And here is the code in the CodeBehind:

public partial class Default : System.Web.UI.Page
    //A simple Person class for example
    class Person
        public string Name
        public int Id
        public Person(string Name, int Id)
            this.Name = Name;
            this.Id = Id;
    protected void Page_Load(object sender, EventArgs e)
        //Prepare some Person objects
        IList<Person> persons = new List<Person>();
        persons.Add(new Person("John",1));
        persons.Add(new Person("Tom",2));
        persons.Add(new Person("Shubho",3));
        //Specify the Name field of Person object as the text field of an item in the drop-down list
        ddlStatus.DataTextField = "Name";
        //Specify the Name field of Person object as the value field of an item in the drop-down list
        ddlStatus.DataValueField = "Id";
        //Assign the persons to the datasource of the drop down list
        ddlStatus.DataSource = persons;
        //Bind the data
        //Now, add a "SysCode" attribute to each item in the dropdown list
        for (int i = 0; i < ddlStatus.Items.Count; i++)
            ListItem item = ddlStatus.Items[i];
            item.Attributes["SysCode"] = "Code-" + (i + 1).ToString();

And, following is the HTML markup that will be generated in the browser:

<select id="ddlStatus" name="ddlStatus">
    <option syscode="Code-1" value="1">John</option>
    <option syscode="Code-2" value="2">Tom</option>
    <option syscode="Code-3" value="3">Shubho</option>

I hope, the example codes are self explanatory and you can understand how to implement your code.

I used an example class to bind data (Person). In reality you should retrieve data from the database and bind the data to the DropDownList using some kind of collections of object.
Member 4044641 19-Aug-10 20:18pm
Thanks, this helped me tremendously
Rate this: bad
Please Sign up or sign in to vote.

Solution 2

sorry for giving answer to old thread. you can do this way.

ListItem test = new ListItem { Text = srText, Value = srValue}
test.Attributes.Add("data-imagesrc", "xxx");
test.Attributes.Add("data-description", "xxx");
Rate this: bad
Please Sign up or sign in to vote.

Solution 3

@Html.LabelFor(model => model.Departamento, htmlAttributes: new { @class = "control-label col-md-2" })

@Html.DropDownList("listDepto", deptos.Select(x => new SelectListItem { Value = x.Id.ToString(), Text = x.Departamento }))
@Html.ValidationMessageFor(model => model.Departamento, "", new { @class = "text-danger" })

NotPolitcallyCorrect 28-Jan-17 16:10pm
Why on earth are you resurrecting SEVEN year old already answered questions? Do those meaningless rep points really mean so much to you?
CHill60 30-Jan-17 7:37am
"Do those meaningless rep points really mean so much to you?" ... obviously not or they would realise that they lose more points than they gain by doing this :D
NotPolitcallyCorrect 30-Jan-17 8:32am
Well to be fair, there are a couple of individuals hanging around that do think that those rep points mean something. One, I ran into yesterday, the other I haven't seen for a little while. But heard a rumor he was still around somewhere. Speaking of which, haven't seen you in a while either.
CHill60 30-Jan-17 8:35am
I've not been about for a few months - personal stuff. Dipped in from my phone from time to time but glad to be back though :-)
NotPolitcallyCorrect 30-Jan-17 9:08am
No worries. Some people I miss.... and some people I don't :-)
CHill60 30-Jan-17 9:55am
Yikes! Hope I was the former ;-)
NotPolitcallyCorrect 30-Jan-17 10:00am
Well, I think you are a valuable contributor so yeah.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month

Advertise | Privacy |
Web01 | 2.8.171114.1 | Last Updated 28 Jan 2017
Copyright © CodeProject, 1999-2017
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100