How to use BulletedList in asp.net

BulletedList web server control

BulletedList is a Standard Toolbox control. It is very easy and essential control. Here i show some basic examples.

Basic Use
First create a Web Form BulletedList.aspx. Then write this code.


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

<!DOCTYPE html>

<script runat="server">

</script>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title>BulletedList Control Example</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <asp:BulletedList ID="BulletedList1" runat="server">
            <asp:ListItem>ColdFusion</asp:ListItem>
            <asp:ListItem>PHP</asp:ListItem>
            <asp:ListItem>Asp.Net</asp:ListItem>
            <asp:ListItem>JSP</asp:ListItem>
        </asp:BulletedList>
    </div>
    </form>
</body>
</html>




Change BulletStyle Property
You can change the BulletStyle property. It's support various format. Here a sample code.


<form id="form1" runat="server">
<div>
 <asp:BulletedList ID="BulletedList1" runat="server" BulletStyle="Numbered">
  <asp:ListItem>ColdFusion</asp:ListItem>
  <asp:ListItem>PHP</asp:ListItem>
  <asp:ListItem>Asp.Net</asp:ListItem>
  <asp:ListItem>JSP</asp:ListItem>
 </asp:BulletedList>
</div>
</form>




DisplayMode: LinkButton
You can use your BulletedList item as a LinkButton. The code is here.


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

<!DOCTYPE html>

<script runat="server">
    protected void BulletedList1_Click(object sender, System.Web.UI.WebControls.BulletedListEventArgs e) {
        Response.Write("You selected: " + BulletedList1.Items[e.Index].Text);
    }
</script>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title>BulletedList Control Example</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <asp:BulletedList ID="BulletedList1" runat="server" BulletStyle="Numbered" DisplayMode="LinkButton" OnClick="BulletedList1_Click">
            <asp:ListItem>ColdFusion</asp:ListItem>
            <asp:ListItem>PHP</asp:ListItem>
            <asp:ListItem>Asp.Net</asp:ListItem>
            <asp:ListItem>JSP</asp:ListItem>
        </asp:BulletedList>
    </div>
    </form>
</body>
</html>




Use SqlDataSource
You can generate your BulletedList Item from a SqlDataSource. First save a ConnectionString in your web.config file name AppConnectionString. Use the NorthWind Database. Then write this code in your BulletedList.aspx page.


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

<!DOCTYPE html>

<script runat="server">

</script>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title>BulletedList Control Example</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <asp:BulletedList ID="BulletedList1" runat="server" 
            DataSourceID="SqlDataSource1" DataTextField="ProductName" 
            DataValueField="ProductName">
        </asp:BulletedList>
        <asp:SqlDataSource ID="SqlDataSource1" runat="server" 
            ConnectionString="<%$ ConnectionStrings:AppConnectionString %>" 
            SelectCommand="SELECT [ProductName] FROM [Products]"></asp:SqlDataSource>
    </div>
    </form>
</body>
</html>




Related asp.net examples