Click here to Skip to main content
Rate this: bad
Please Sign up or sign in to vote.
See more: ASP.NET VB.NET
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 14:04pm

1 solution

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 at 19-Aug-10 20:18pm
Thanks, this helped me tremendously

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

  Print Answers RSS
0 Sergey Alexandrovich Kryukov 458
1 OriginalGriff 384
2 George Jonsson 233
3 Animesh Datta 130
4 Shemeemsha RA 128
0 OriginalGriff 6,179
1 Sergey Alexandrovich Kryukov 5,616
2 CPallini 4,770
3 George Jonsson 3,400
4 Gihan Liyanage 2,522

Advertise | Privacy | Mobile
Web03 | 2.8.140916.1 | Last Updated 18 Aug 2010
Copyright © CodeProject, 1999-2014
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