How to add an item to BulletedList programmatically

Add an item to BulletedList programmatically
BulletedList is an asp.net list web server control. ASP.NET developers use BulletedList server control to create a list of items that are formatted with bullets. BulletedList Items property gets the collection of items in the bulletedlist control. Items collection contains all ListItem objects. A ListItem object represents a single item in bulletedlist control.

ListItem object have three properties thats are Text, Value and Selected. Text property text display in BulletedList control and Value property text is associated with ListItem and it is hidden in browser. Selected property indicate whether ListItem is selected or not.

We can add ListItem objects of BulletedList server control at design time. We also can populate BulletedList by data binding with data source control. Even we can add items to BulletedList control programmatically at run time.

To add an item programmatically in BulletedList control, first we need to create a ListItem object. Next we need to add the ListItem object in Items collection of BulletedList control. Now BulletedList will display newly added item.

The following c# example code demonstrate us how can we add item in BulletedList server control programmatically (dynamically) at run time in asp.net.
BulletedListAddItem.aspx

<%@ Page Language="C#" %>

<!DOCTYPE html>

<script runat="server">
    protected void Button1_Click(object sender, System.EventArgs e)
    {
        ListItem li = new ListItem();
        li.Text = TextBox1.Text.ToString();
        BulletedList1.Items.Add(li);
        Label1.Text = "ListItem added in BulletedList: " + li.Text;
    }
</script>

<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
    <title>How to add list item in BulletedList programmatically</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <h2 style="color:Red">BulletedList example: Add List Item</h2>
        <asp:Label 
             ID="Label1" 
             runat="server"
             Font-Bold="true"
             ForeColor="SeaGreen"
             Font-Size="Large"
             >
        </asp:Label>
        <br /><br />
        <asp:Label 
             ID="Label2" 
             runat="server" 
             Text="Color List"
             Font-Bold="true"
             ForeColor="DodgerBlue"
             >
        </asp:Label>
        <br />
        <asp:BulletedList 
             ID="BulletedList1"
             runat="server"
             BackColor="DodgerBlue"
             ForeColor="Snow"
             Width="250"
             >
             <asp:ListItem>Magenta</asp:ListItem>
             <asp:ListItem>MediumBlue</asp:ListItem>
             <asp:ListItem>Orchid</asp:ListItem>
             <asp:ListItem>PaleGoldenRod</asp:ListItem>
             <asp:ListItem>Maroon</asp:ListItem>
        </asp:BulletedList>
        <br />
        <asp:Label 
             ID="Label3" 
             runat="server"
             ForeColor="DodgerBlue"
             Text="Item Text"
             >
        </asp:Label>
        <asp:TextBox 
             ID="TextBox1"
             runat="server"
             BackColor="DodgerBlue"
             ForeColor="Snow"
             >
        </asp:TextBox>
        <br />
        <asp:Button 
             ID="Button1" 
             runat="server"
             Text="Add List Item"
             Font-Bold="true"
             ForeColor="DodgerBlue"
             OnClick="Button1_Click"
             />
    </div>
    </form>
</body>
</html>
More asp.net examples