Click here to Skip to main content
15,886,362 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
hi guys

i have problems in getting custom attribute of list box items

i have select quarry that select rows and save in DataTable Named "dt"

i add all of items in list box and set custom attribute "type"

C#
foreach (DataRow ro in dt2.Rows)
            {
                li = new ListItem();
                li.Value = ro["fileOuId"].ToString();
                li.Text = ro["EsTittleF"].ToString();
                li.Attributes.Add("type", "Essay");
                ListBox1.Items.Add(li);
            }


i want to get type attribute on list box select item change

C#
 protected void ListBox1_SelectedIndexChanged(object sender, EventArgs e)
    {
       

       

        string type = "";
        type=ListBox1.SelectedItem.Attributes["type"];

        
        if (type=="Book" )
             // Book
        if (type=="Essay")
             //essay
}


the problem is : "string type" is null in item change ,,, i can see type in each item in execute mode;

HTML
<option type="Essay" value="242">Esssssssssssssssssay test </option>
Posted

1 solution

Actually this is a common issue documented on various sites and forums. When adding ListItems dynamically and setting attributes that the attribute tags are not retained during postback.

We need to derive a control from the base of ListBox and implement the methods to render it properly.Here is an article that describes how to do this and covers this issue(for dropdownlist).

Please Click here....

The article covers the DropdownBox but the ListItem class is the same class for dropdownlists as it is for the listbox control.

Hope this will definitely of help for you.
 
Share this answer
 

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