In this article I will explain with an example, how to filter the items in
DataList control based on the selected value in DropDownList control in ASP.Net using C# and VB.Net.
Database
Here I am making use of Microsoft’s Northwind Database. You can download it from here.
HTML Markup
The following HTML Markup consists of:
DropDownList – For displaying countries.
The DropDownList consists of ListItem.
DataList – For displaying data.
The
DataList consists of
ItemTemplate which contains
Eval function to bind columns from database.
Country:
<asp:DropDownList ID="ddlCountry" AutoPostBack="true" runat="server">
<asp:ListItem Text="All" Value="" />
<asp:ListItem Text="Argentina" Value="Argentina" />
<asp:ListItem Text="Belgium" Value="Belgium" />
<asp:ListItem Text="Brazil" Value="Brazil" />
<asp:ListItem Text="Canada" Value="Canada" />
</asp:DropDownList><br />
<asp:DataList ID="dlCustomers" runat="server" RepeatLayout="Table" RepeatColumns="3" CellPadding="2" CellSpacing="2">
<ItemTemplate>
<table cellpadding="2" cellspacing="0" border="1" style="width: 200px; height: 100px; border: dashed 2px #F7F7F7; background-color: #F7F7F7">
<tr>
<td>
<b><u><span class="name"><%# Eval("ContactName") %></span></u></b>
</td>
</tr>
<tr>
<td>
<b>City: </b><span class="city"><%# Eval("City") %></span><br />
<b>Postal Code: </b><span class="postal"><%# Eval("PostalCode") %></span><br />
<b>Country: </b><span class="country"><%# Eval("Country")%></span><br />
<b>Phone: </b><span class="phone"><%# Eval("Phone")%></span><br />
<b>Fax: </b><span class="fax"><%# Eval("Fax")%></span><br />
</td>
</tr>
</table>
</ItemTemplate>
</asp:DataList>
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
Binding and Filtering the Items of the ASP.Net DataList Control
Inside the Page_Load event handler, the records are fetched from the Customers Table of the Northwind database.
Then, a DropDownList’s Country parameter is added to a SqlCommand.
C#
protected void Page_Load(object sender, EventArgs e)
{
string sql = "SELECT ContactName, City, PostalCode, Country, Phone, Fax FROM Customers WHERE Country=@Country or @Country=''";
string constr = ConfigurationManager.ConnectionStrings["constr"].ConnectionString;
using (SqlConnection con = new SqlConnection(constr))
{
using (SqlCommand cmd = new SqlCommand(sql, con))
{
using (SqlDataAdapter sda = new SqlDataAdapter(cmd))
{
cmd.Parameters.AddWithValue("@Country", ddlCountry.SelectedItem.Value);
using (DataSet ds = new DataSet())
{
sda.Fill(ds);
dlCustomers.DataSource = ds;
dlCustomers.DataBind();
}
}
}
}
}
VB.Net
Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Load
Dim sql As String = "SELECT ContactName, City, PostalCode, Country, Phone, Fax FROM Customers WHERE Country=@Country or @Country=''"
Dim constr As String = ConfigurationManager.ConnectionStrings("constr").ConnectionString
Using con As SqlConnection = New SqlConnection(constr)
Using cmd As SqlCommand = New SqlCommand(sql, con)
Using sda As SqlDataAdapter = New SqlDataAdapter(cmd)
cmd.Parameters.AddWithValue("@Country", ddlCountry.SelectedItem.Value)
Using ds As DataSet = New DataSet()
sda.Fill(ds)
dlCustomers.DataSource = ds
dlCustomers.DataBind()
End Using
End Using
End Using
End Using
End Sub
Screenshot
Demo
Downloads