Refer the below sample code for your reference you can use Updatepanel or just GridView with filter option dropdown it will work in both the cases.
I have use Northwind Database to create a sample.
SQL
-- EXEC GetCustomersBasedOnCountry NULL
-- EXEC GetCustomersBasedOnCountry 'Canada'
CREATE PROCEDURE GetCustomersBasedOnCountry
@Filter VARCHAR(50) = NULL
AS
BEGIN
SET NOCOUNT ON;
SELECT ContactName
,City
,Country
,PostalCode
FROM Customers
WHERE Country = @Filter
OR @Filter IS NULL
END
GO
HTML
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<asp:ScriptManager ID="ScriptManager1" runat="server">
</asp:ScriptManager>
<div>
Country :
<asp:DropDownList ID="ddlCountry" runat="server" OnSelectedIndexChanged="CountryChanged"
AutoPostBack="true" AppendDataBoundItems="true">
<asp:ListItem Text="Select Country" Value=""></asp:ListItem>
<asp:ListItem Text="Argentina" Value="Argentina"></asp:ListItem>
<asp:ListItem Text="Austria" Value="Austria"></asp:ListItem>
<asp:ListItem Text="Belgium" Value="Belgium"></asp:ListItem>
<asp:ListItem Text="Brazil" Value="Brazil"></asp:ListItem>
<asp:ListItem Text="Canada" Value="Canada"></asp:ListItem>
</asp:DropDownList>
<br />
<br />
<asp:UpdatePanel ID="UpdatePanel1" runat="server">
<ContentTemplate>
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="false" AllowPaging="true"
PageSize="2" Font-Names="Arial" Font-Size="11pt" OnPageIndexChanging="OnPaging">
<Columns>
<asp:BoundField DataField="ContactName" HeaderText="Contact Name" />
<asp:BoundField DataField="City" HeaderText="City" />
<asp:BoundField DataField="Country" HeaderText="Country" />
<asp:BoundField DataField="PostalCode" HeaderText="Postal Code" />
</Columns>
</asp:GridView>
</ContentTemplate>
</asp:UpdatePanel>
</div>
</form>
</body>
</html>
C#
protected void Page_Load(object sender, EventArgs e)
{
if (!this.IsPostBack)
{
BindGrid();
}
}
protected void OnPaging(object sender, GridViewPageEventArgs e)
{
GridView1.PageIndex = e.NewPageIndex;
this.BindGrid(ddlCountry.SelectedValue);
}
protected void CountryChanged(object sender, EventArgs e)
{
GridView1.PageIndex = 0;
this.BindGrid(ddlCountry.SelectedValue);
}
private void BindGrid(string country = "")
{
DataTable dt = new DataTable();
String strConnString = ConfigurationManager.ConnectionStrings["ConStr1"].ConnectionString;
SqlConnection con = new SqlConnection(strConnString);
SqlDataAdapter sda = new SqlDataAdapter();
SqlCommand cmd = new SqlCommand("GetCustomersBasedOnCountry");
cmd.CommandType = CommandType.StoredProcedure;
if (ddlCountry.SelectedItem.Value != "")
{
cmd.Parameters.AddWithValue("@Filter", ddlCountry.SelectedItem.Value);
}
cmd.Connection = con;
sda.SelectCommand = cmd;
sda.Fill(dt);
GridView1.DataSource = dt;
GridView1.DataBind();
}
VB.Net
Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Load
If Not Me.IsPostBack Then
BindGrid()
End If
End Sub
Protected Sub OnPaging(ByVal sender As Object, ByVal e As GridViewPageEventArgs)
GridView1.PageIndex = e.NewPageIndex
Me.BindGrid(ddlCountry.SelectedValue)
End Sub
Protected Sub CountryChanged(ByVal sender As Object, ByVal e As EventArgs)
GridView1.PageIndex = 0
Me.BindGrid(ddlCountry.SelectedValue)
End Sub
Private Sub BindGrid(Optional ByVal country As String = "")
Dim dt As DataTable = New DataTable()
Dim strConnString As String = ConfigurationManager.ConnectionStrings("ConStr1").ConnectionString
Dim con As SqlConnection = New SqlConnection(strConnString)
Dim sda As SqlDataAdapter = New SqlDataAdapter()
Dim cmd As SqlCommand = New SqlCommand("GetCustomersBasedOnCountry")
cmd.CommandType = CommandType.StoredProcedure
If ddlCountry.SelectedItem.Value <> "" Then
cmd.Parameters.AddWithValue("@Filter", ddlCountry.SelectedItem.Value)
End If
cmd.Connection = con
sda.SelectCommand = cmd
sda.Fill(dt)
GridView1.DataSource = dt
GridView1.DataBind()
End Sub
Screenshot
