C#

Data Binding Using BulletedList Control

ASP.NET provides several server-side controls that allow data binding. Earlier I have discussed the basics of Data Binding in ASP.NET and demonstrated it using DropDownList Control. In this article, I will explain Data Binding Using BulletedList Control which is one of the List Control available in ASP.NET.

Before proceeding with data binding using BulletedList Control, let us first discuss some important points regarding this control.

BulletedList Control in ASP.NET

  • Basically, it is a server-side control.
  • Like HTML bulleted list, the ASP.NET also has similar list control that formats the list items with bullets.
  • Each member of the BulletedList control belongs to the type ListItem.
  • The following types of styles are available for the bullets using the BulletStyle property.
    • NotSet when you don’t want to show bullets.
    • Numbered for displaying the numbers rather than bullets.
    • LowerRoman
    • UpperRoman
    • LowerAlpha
    • UpperAlpha
    • Disc
    • Circle
    • Square
    • CustomImage
  • Also, it is possible to change the starting number using the FirstBulletNumber property.
  • Besides, it has a DisplayMode property to show the items as Text, Hyperlink, or LinkButton.
  • Like DropDownList control, it has the same properties for data binding.

Example of Data Binding Using BulletedList Control

The following example demonstrates the data binding using a database table as the data source. In this example, we use a table named Colors. Further, this table has two columns – color_id, and color_name.

Like before, we fetch the data of this table in a Dataset object. As shown below, the Connect() method creates a connection with the database using a connection string which is defined in the web.config file. Also, open the connection using the Open() method of the SqlConnection object.

Further, the BuildList() method creates the BulletedList using the data retrieved from the Colors table. In fact, the data binding takes place when we assign the dataset object to the DataSource property of the list and call the DataBind() method.

After that, we use a TextBox to allow users to add a color name. Whenever the user enters a value in the TextBox and presses the Enter key, the TextChanged event of the textBox fires, and the corresponding event handler function executes. We obtain the next value of color_id using the GetNextId() method. Finally, a parameterized query is executed to add a row in the table and the BuildList() method is called again.

using System;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;

namespace BulletedListExample
{
    public partial class WebForm1 : System.Web.UI.Page
    {
        SqlConnection c;
        SqlDataAdapter da;
        DataSet ds;
        public void Connect()
        {
            c = new SqlConnection(ConfigurationManager.ConnectionStrings["con1"].ToString());
            c.Open();
        }
        public void BuildList()
        {
            Connect();
            da = new SqlDataAdapter("select * from Colors", c);
            ds = new DataSet();
            da.Fill(ds, "Colors");
            BulletedList1.DataSource = ds;
            BulletedList1.DataTextField = "color_name";
            BulletedList1.DataValueField = "color_id";
            BulletedList1.DataBind();
            c.Close();
        }

        public int GetNextId()
        {
            Connect();
            SqlCommand cmd = new SqlCommand("select max(color_id) from Colors", c);
            int n = (Int32)cmd.ExecuteScalar();
            c.Close();
            return ++n;
        }
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
                BuildList();
        }

        protected void TextBox1_TextChanged(object sender, EventArgs e)
        {
            String str = TextBox1.Text;
            if (!String.IsNullOrEmpty(str))
            {
                int x = GetNextId();
                string s = "insert into Colors values(@id, @name)";
                Connect();
                SqlCommand cmd = new SqlCommand();
                cmd.Connection = c;
                cmd.CommandText = s;

                cmd.Parameters.AddWithValue("@id", x);
                cmd.Parameters.AddWithValue("@name", str);
                cmd.ExecuteNonQuery();
                c.Close();
                BuildList();
                TextBox1.Text = "";
            }
        }
    }
}

Output

An Example of Data Binding Using BulletedList Control
An Example of Data Binding Using BulletedList Control

Database Table

Database Table Used as Data Source for the Bulleted List
Database Table Used as Data Source for the Bulleted List

In this example, the data source being used is a SQL Server database. However, it is also possible to use an in-memory database as the data source which will be demonstrated in the next example.

Summary

This article on Data Binding Using BulletedList Control, describes the BulletedList control and some of its important properties. Also, an example of data binding is also shown using the BulletedList control.


You may also like...

Leave a Reply

Your email address will not be published. Required fields are marked *