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" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN""http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<script runat="server">

Public Class CartItem
Private _id As Integer
Public _description As String
Public ReadOnly Property Id() As Integer
Get
Return _id
End Get
End
Property

Public ReadOnly Property Description() As String
Get
Return _description
End Get
End
Property

Public Sub New(ByVal id As Integer, ByVal description As String)
_id = id
_description = description
End Sub
End
Class

Private Sub Page_Load()
If Not IsPostBack
Then

Dim shoppingCart As New List(Of CartItem)()
shoppingCart.Add(New CartItem(1, "Computer"))
shoppingCart.Add(New CartItem(2, "Television"))
shoppingCart.Add(New CartItem(3, "Boofer"))

lstShoppingCart.DataSource = shoppingCart
lstShoppingCart.DataBind()
End If
End
Sub
</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:ListBox
id="lstShoppingCart"
DataTextField="Description"
DataValueField="Id"
Runat="server" />
</div>
</form
>
</body>
</
html>


In above example, ListBox bound to the collection in the Page_Load() method. Notice that the DataTextField and DataValueField properties of the ListBox control represent properties of the CartItem class. A List control's DataTextField and DataValueField properties can refer to any public property of a class, but you cannot bind a List control to a public field.

Note: Continue in Next Part.

HAVE A GREAT CODING!