13

I need to produce with asp.net controls this structure, but ListItem doesn't allow add properties and classes.

What is the best way to do it?

<ul>
 <li class="1">SomeText</li>
 <li class="2">SomeText2</li>
</ul>

4 Answers 4

28

You can pass-through class attribute:

<asp:BulletedList ID="BulletedList1" runat="server">
  <asp:ListItem class="1">SomeText</asp:ListItem>
  <asp:ListItem class="2">SomeText2</asp:ListItem>
</asp:BulletedList>

. . .

protected void Page_Load(object sender, EventArgs e)
{
   ListItem listItem = new ListItem("Test 3");
   listItem.Attributes.Add("class", "3");
   BulletedList1.Items.Add(listItem);
}
Sign up to request clarification or add additional context in comments.

Comments

4

You can still add custom attributes:

// assuming li is your WebControl or HtmlControl:
li.Attributes.Add("class", "1");

1 Comment

How can he specify different classes for each list item with this code?
1

This works fine for me, but using dropdownlist and VB

    Dim ListItem As ListItem = New ListItem("All folders", 0)
    ListItem.Attributes.Add("style", "color:red;")
    DropDownListFolders.Items.Add(ListItem)

Comments

1

Dropdown in listview -- if you want to give popup window on selecting item when dropdown textsize is fixed:

protected void lstViewVehicle_ItemCreated(object sender, ListViewItemEventArgs e)
{        
    try
    {
    if (e.Item.ItemType == ListViewItemType.InsertItem)
    {
        DropDownList ddl = (DropDownList)e.Item.FindControl("ddlDescription");
        if (ddl != null)
        {
            string description = exp_Type_Vehicle;
            clsBER objclsBER = new clsBER();
            DataSet ds = objclsBER.FillDropdown(description);

            foreach (DataRow dr in ds.Tables[0].Rows)
            {
                ListItem lstitem = new ListItem(dr["expense_description"].ToString(), dr["eid"].ToString());
                lstitem.Attributes.Add("title", dr["expense_description"].ToString());
                //lstitem.Attributes.Add("style", "color:blue");
                ddl.Items.Add(lstitem);
            }
            ddl.DataBind();
        }
    }
    }
    catch (Exception ex)
    {
        (new csComman()).dealWithEx(ex, Session);
        Response.Redirect("ErrorPage/ErrorPage.aspx", false);
    }   
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.