Programmingempire
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
Database Table
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.
Further Reading
How to Create Instance Variables and Class Variables in Python
Comparing Rows of Two Tables with ADO.NET
Example of Label and Textbox Control in ASP.NET
One Dimensional and Two Dimensuonal Indexers in C#
Private and Static Constructors in C#
Programs to Find Armstrong Numbers in C#
One Dimensional and Two Dimensional Indexers in C#
Generic IList Interface and its Implementation in C#
Creating Navigation Window Application Using WPF in C#
Find Intersection Using Arrays
An array of Objects and Object Initializer
Performing Set Operations in LINQ
Data Binding Using BulletedList Control
Understanding the Quantifiers in LINQ
Deferred Query Execution and Immediate Query Execution in LINQ
Examples of Query Operations using LINQ in C#
An array of Objects and Object Initializer
Language-Integrated Query (LINQ) in C#
Examples of Connected and Disconnected Approach in ADO.NET
IEnumerable and IEnumerator Interfaces
KeyValuePair and its Applications
Learning All Class Members in C#
Examples of Extension Methods in C#
How to Setup a Connection with SQL Server Database in Visual Studio
Understanding the Concept of Nested Classes in C#
A Beginner’s Tutorial on WPF in C#
Explaining C# Records with Examples
Everything about Tuples in C# and When to Use?
Linear Search and Binary search in C#
Examples of Static Constructors in C#