user3590502
user3590502

Reputation: 11

How to add an extra Attribute in the DropDown List in c#

I have a drop down in which I am binding a List like:

var ddlLst2 = new List<string> { "Audi", "BMW", "Ford", "Vauxhall"};
ddlLst2.Sort();

foreach (var item in ddlLst2)
{
    DropDownList2.Items.Add(new ListItem(item));

}

Result:

<select id="DropDownList2" onchange="javascript:setTimeout('__doPostBack(\'DropDownList2\',\'\')', 0)" name="DropDownList2">

    <option value="--Please Select--" selected="selected"></option>
    <option value="Audi"></option>
    <option value="BMW"></option>
    <option value="Ford"></option>
    <option value="Vauxhall"></option>

</select>

But I want to add an attribute in each item add expected the outcome like below:

Expected Result:

<select id="DropDownList2" onchange="javascript:setTimeout('__doPostBack(\'DropDownList2\',\'\')', 0)" name="DropDownList2">

    <option value="--Please Select--" selected="selected"></option>
    <option value="Audi" code="1"></option>
    <option value="BMW" code="2"></option>
    <option value="Ford" code="3"></option>
    <option value="Vauxhall" code="4"></option>

</select>

As you can see above there is an extra attribute added "code", the value of the code comes from the query.

Upvotes: 0

Views: 3181

Answers (4)

protected void dropdownlist_DataBound(object sender, EventArgs e)
    {
        DataTable dt = new DataTable();
        DataView dv = (DataView)sqldatasource.Select(DataSourceSelectArguments.Empty);
        dt = dv.ToTable();

        foreach (DataRow dr in dt.Rows)
        {
            dropdownlist.Items.FindByValue(dr[0].ToString()).Attributes.Add("attr0", dr[2].ToString());
            dropdownlist.Items.FindByValue(dr[0].ToString()).Attributes.Add("attr1", dr[3].ToString());
            dropdownlist.Items.FindByValue(dr[0].ToString()).Attributes.Add("attr2", dr[4].ToString());

        }

    }

Upvotes: 0

Martin Braun
Martin Braun

Reputation: 12589

For the JS event: You can subscripe to it from your javascript. I.e. with JQuery: $('#DropDownList2').change(function() { alert("Fired!"); });

Upvotes: 0

Neville Bamshoot
Neville Bamshoot

Reputation: 143

void Main()
{
var ddlLst2 = new List<string> { "Audi", "BMW", "Ford", "Vauxhall"};
        ddlLst2.Sort();

        var ctrls = new List<ListItem>();
        int i =0;
        foreach (var item in ddlLst2)
        {
            //DropDownList2.Items.Add(new ListItem(item));
            ListItem li = new ListItem(item);
            li.Attributes.Add("code", i.ToString());
            ctrls.Add(li);
            i++;
        }

        foreach (ListItem element in ctrls)
        {
            Console.WriteLine(element.Attributes["code"]);
        }
}

Upvotes: 1

MaxOvrdrv
MaxOvrdrv

Reputation: 1916

foreach (var item in ddlLst2)
            {
                ListItem tmp = new ListItem();
                tmp.Text = item.ToString();
                tmp.Attributes.Add("AttribName", "AttribValue");
                DropDownList2.Items.Add(tmp);

            }

or something like that... play around with the available properties of tmp...

Upvotes: 1

Related Questions