Click here to Skip to main content
12,509,899 members (43,544 online)
Rate this:
 
Please Sign up or sign in to vote.
See more: VB 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( ))
ddlStatus.DataBind()

but the attribute is not bound to the control
Posted 18-Aug-10 14:04pm
Rate this: bad
 
good
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">
        </asp:DropDownList>

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
        {
            get;
            set;
        }
        public int Id
        {
            get;
            set;
        }
        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
        ddlStatus.DataBind();
 
        //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>
</select>

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.
  Permalink  
Comments
Member 4044641 19-Aug-10 20:18pm
   
Thanks, this helped me tremendously
Rate this: bad
 
good
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");
dropListUserImages.Items.Add(test);
  Permalink  

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 | Mobile
Web01 | 2.8.160929.1 | Last Updated 16 Feb 2015
Copyright © CodeProject, 1999-2016
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