Introduction
In Part
- 1 article of this series, we
have discussed how to declare list items but still we have not discussed how to
bind it to databases. Let's take a look on it.
Binding to Data Source
We can bind any of the List controls to a data source. The List controls support
both declarative databinding and programmatic databinding.
Declarative Databinding
The page given below contains a DropDownList, ListBox and BulletList controls
that is bound to the subject database table with declarative databinding.
<%@ Page Language="VB" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN""http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<script runat="server">
</script>
<html xmlns="http://www.w3.org/1999/xhtml" >
<head id="Head1" runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:DropDownList
id="ddlSubjects"
DataSourceID="SqlDataSource1"
DataTextField="SUBJECT"
DataValueField="ID"
Runat="server" />
<br />
<br />
<asp:ListBox
ID="lstSubject"
DataSourceID="SqlDataSource1"
DataTextField="SUBJECT"
DataValueField="ID"
runat="server"/>
<br />
<br />
<asp:BulletedList
ID="bltSubject"
DataSourceID="SqlDataSource1"
DataTextField="SUBJECT"
DataValueField="ID"
runat="server" />
<br />
<br />
<br />
<asp:SqlDataSource ID="SqlDataSource1" runat="server"
ConnectionString="<%$ ConnectionStrings:DatabaseConnectionString1 %>"
ProviderName="<%$ ConnectionStrings:DatabaseConnectionString1.ProviderName %>"
SelectCommand="SELECT
[ID], [SUBJECT] FROM [SUBJECT]"></asp:SqlDataSource>
</div>
</form>
</body>
</html>
Notice that the DropDownList, ListBox and BulletList control's DataSourceID
property points to the ID of the SqlDataSource control. When we open the page
the SqlDataSource control retrieves the records from the subject database table.
Programmatic Databinding
As an alternative to declarative databinding, we can programmatically bind any
of the List controls to a data source. Take a look at the example given below.
<%@ Page Language="VB" %>

Join the conversation! Your thoughts help the community grow.