Click here to Skip to main content
15,881,248 members
Please Sign up or sign in to vote.
5.00/5 (1 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( ))
ddlStatus.DataBind()

but the attribute is not bound to the control
Posted
Updated 28-Jan-17 9:39am

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:

XML
<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:

XML
<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.
 
Share this answer
 
Comments
Member 4044641 19-Aug-10 20:18pm    
Thanks, this helped me tremendously
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);
 
Share this answer
 

@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" })

 
Share this answer
 
Comments
[no name] 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
[no name] 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 :-)
[no name] 30-Jan-17 9:08am    
No worries. Some people I miss.... and some people I don't :-)

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



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900