In this article I will explain with an example, how to populate (bind) DropDownList in ItemTemplate of Repeater Control in ASP.Net using C# and VB.Net.
The DropDownList will be populated from database inside the OnItemDataBound event of the Repeater control in ASP.Net using C# and VB.Net.
Database
Here I am making use of Microsoft’s Northwind Database. The download and install instructions are provided in the following article.
HTML Markup
The following HTML Markup consists of an ASP.Net Repeater control with an HTML Table Layout.
The Repeater’s ItemTemplate consists of two Label controls and a DropDownList control.
<asp:Repeater ID="rptCustomers" runat="server" OnItemDataBound="OnItemDataBound">
<HeaderTemplate>
<table cellspacing="0" rules="all" border="1">
<tr>
<th scope="col" style="width: 80px">
Customer Id
</th>
<th scope="col" style="width: 120px">
Name
</th>
<th scope="col" style="width: 100px">
Country
</th>
</tr>
</HeaderTemplate>
<ItemTemplate>
<tr>
<td>
<asp:Label ID="lblCustomerId" runat="server" Text='<%# Eval("CustomerId") %>' />
</td>
<td>
<asp:Label ID="lblName" runat="server" Text='<%# Eval("ContactName") %>' />
</td>
<td>
<asp:DropDownList ID="ddlCountries" runat="server">
</asp:DropDownList>
</td>
</tr>
</ItemTemplate>
<FooterTemplate>
</table>
</FooterTemplate>
</asp:Repeater>
Namespaces
You will need to import the following namespaces.
C#
using System.Data;
using System.Data.SqlClient;
using System.Configuration;
VB.Net
Imports System.Data
Imports System.Data.SqlClient
Imports System.Configuration
Populating Repeater control from database in ASP.Net
Inside the Page load event handler, the Repeater is populated with the records from the Customers table.
C#
protected void Page_Load(object sender, EventArgs e)
{
if (!this.IsPostBack)
{
rptCustomers.DataSource = this.GetData("SELECT TOP 10 CustomerId, ContactName, Country FROM Customers");
rptCustomers.DataBind();
}
}
private DataTable GetData(string query)
{
string constr = ConfigurationManager.ConnectionStrings["constr"].ConnectionString;
using (SqlConnection con = new SqlConnection(constr))
{
using (SqlCommand cmd = new SqlCommand(query, con))
{
using (SqlDataAdapter sda = new SqlDataAdapter(cmd))
{
DataTable dt = new DataTable();
sda.Fill(dt);
return dt;
}
}
}
}
VB.Net
Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Load
If Not Me.IsPostBack Then
rptCustomers.DataSource = Me.GetData("SELECT TOP 10 CustomerId, ContactName, Country FROM Customers")
rptCustomers.DataBind()
End If
End Sub
Private Function GetData(ByVal query As String) As DataTable
Dim constr As String = ConfigurationManager.ConnectionStrings("constr").ConnectionString
Using con As SqlConnection = New SqlConnection(constr)
Using cmd As SqlCommand = New SqlCommand(query, con)
Using sda As SqlDataAdapter = New SqlDataAdapter(cmd)
Dim dt As DataTable = New DataTable()
sda.Fill(dt)
Return dt
End Using
End Using
End Using
End Function
Populate (Bind) DropDownList in ItemTemplate of Repeater Control in ASP.Net
Inside the OnItemDataBound event handler, first the DropDownList is referenced using the FindControl method and then it is populated with records of Countries from database.
The Country value of the respective Customer is fetched from the DataItem object of the Repeater and is used to select the Country inside the DropDownList control.
C#
protected void OnItemDataBound(object sender, RepeaterItemEventArgs e)
{
if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
{
//Find the DropDownList in the Repeater Item.
DropDownList ddlCountries = (e.Item.FindControl("ddlCountries") as DropDownList);
ddlCountries.DataSource = this.GetData("SELECT DISTINCT Country FROM Customers");
ddlCountries.DataTextField = "Country";
ddlCountries.DataValueField = "Country";
ddlCountries.DataBind();
//Add Default Item in the DropDownList.
ddlCountries.Items.Insert(0, new ListItem("Please select"));
//Select the Country of Customer in DropDownList.
string country = (e.Item.DataItem as DataRowView)["Country"].ToString();
ddlCountries.Items.FindByValue(country).Selected = true;
}
}
VB.Net
Protected Sub OnItemDataBound(ByVal sender As Object, ByVal e As RepeaterItemEventArgs)
If e.Item.ItemType = ListItemType.Item OrElse e.Item.ItemType = ListItemType.AlternatingItem Then
'Find the DropDownList in the Repeater Item.
Dim ddlCountries As DropDownList = (TryCast(e.Item.FindControl("ddlCountries"), DropDownList))
ddlCountries.DataSource = Me.GetData("SELECT DISTINCT Country FROM Customers")
ddlCountries.DataTextField = "Country"
ddlCountries.DataValueField = "Country"
ddlCountries.DataBind()
'Add Default Item in the DropDownList.
ddlCountries.Items.Insert(0, New ListItem("Please select"))
'Select the Country of Customer in DropDownList.
Dim country As String = (TryCast(e.Item.DataItem, DataRowView))("Country").ToString()
ddlCountries.Items.FindByValue(country).Selected = True
End If
End Sub
Screenshot
Demo
Downloads