Hi KanKon,
You are calling the BindGrid method before declaring the ViewState In postback. You need to call the BindGrid Method after declaring the ViewState.
Please refer below sample.
HTML
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="false" AllowPaging="true"
PageSize="10" Font-Names="Arial" Font-Size="11pt" AlternatingRowStyle-BackColor="#C2D69B"
HeaderStyle-BackColor="green" OnPageIndexChanging="OnPaging">
<Columns>
<asp:BoundField DataField="ContactName" HeaderText="Contact Name" />
<asp:BoundField DataField="City" HeaderText="City" />
<asp:TemplateField>
<HeaderTemplate>
Country:
<asp:DropDownList ID="MaxCountry" runat="server" OnSelectedIndexChanged="CountryChanged"
AutoPostBack="true" AppendDataBoundItems="true">
<asp:ListItem Text="ALL" Value="ALL"></asp:ListItem>
<asp:ListItem Text="Top 10" Value="10"></asp:ListItem>
</asp:DropDownList>
</HeaderTemplate>
<ItemTemplate>
<asp:Label ID="lblCountry" runat="server" Text='<%# Eval("Country") %>'></asp:Label>
<asp:Label ID="lblContactName" runat="server" Text='<%# Eval("ContactName") %>'></asp:Label>
<asp:Label ID="lblCity" runat="server" Text='<%# Eval("City") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:BoundField DataField="PostalCode" HeaderText="Postal Code" />
</Columns>
</asp:GridView>
Namespaces
C#
using System.Data;
using System.Data.SqlClient;
VB.Net
Imports System.Data
Imports System.Data.SqlClient
Code
C#
protected void Page_Load(object sender, EventArgs e)
{
if (!this.IsPostBack)
{
ViewState["Filter"] = "ALL";
this.BindGrid();
}
}
private void BindGrid()
{
DataTable dt = new DataTable();
String strConnString = System.Configuration.ConfigurationManager.ConnectionStrings["conString"].ConnectionString;
SqlConnection con = new SqlConnection(strConnString);
SqlDataAdapter sda = new SqlDataAdapter();
SqlCommand cmd = new SqlCommand("spx_GetCustomers");
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.AddWithValue("@Filter", ViewState["Filter"].ToString());
cmd.Connection = con;
sda.SelectCommand = cmd;
sda.Fill(dt);
GridView1.DataSource = dt;
GridView1.DataBind();
DropDownList MaxCountry = (DropDownList)GridView1.HeaderRow.FindControl("MaxCountry");
this.BindCountryList(MaxCountry);
}
private void BindCountryList(DropDownList MaxCountry)
{
String strConnString = System.Configuration.ConfigurationManager.ConnectionStrings["conString"].ConnectionString;
SqlConnection con = new SqlConnection(strConnString);
SqlDataAdapter sda = new SqlDataAdapter();
SqlCommand cmd = new SqlCommand("select distinct Country" + " from customers");
cmd.Connection = con;
con.Open();
MaxCountry.DataSource = cmd.ExecuteReader();
MaxCountry.DataTextField = "Country";
MaxCountry.DataValueField = "Country";
MaxCountry.DataBind();
con.Close();
MaxCountry.Items.FindByValue(ViewState["Filter"].ToString()).Selected = true;
}
protected void CountryChanged(object sender, EventArgs e)
{
DropDownList MaxCountry = (DropDownList)sender;
ViewState["Filter"] = MaxCountry.SelectedValue;
this.BindGrid();
}
protected void OnPaging(object sender, GridViewPageEventArgs e)
{
GridView1.PageIndex = e.NewPageIndex;
this.BindGrid();
}
VB.Net
Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Load
If Not Me.IsPostBack Then
ViewState("Filter") = "ALL"
Me.BindGrid()
End If
End Sub
Private Sub BindGrid()
Dim dt As DataTable = New DataTable()
Dim strConnString As String = System.Configuration.ConfigurationManager.ConnectionStrings("conString").ConnectionString
Dim con As SqlConnection = New SqlConnection(strConnString)
Dim sda As SqlDataAdapter = New SqlDataAdapter()
Dim cmd As SqlCommand = New SqlCommand("spx_GetCustomers")
cmd.CommandType = CommandType.StoredProcedure
cmd.Parameters.AddWithValue("@Filter", ViewState("Filter").ToString())
cmd.Connection = con
sda.SelectCommand = cmd
sda.Fill(dt)
GridView1.DataSource = dt
GridView1.DataBind()
Dim MaxCountry As DropDownList = CType(GridView1.HeaderRow.FindControl("MaxCountry"), DropDownList)
Me.BindCountryList(MaxCountry)
End Sub
Private Sub BindCountryList(ByVal MaxCountry As DropDownList)
Dim strConnString As String = System.Configuration.ConfigurationManager.ConnectionStrings("conString").ConnectionString
Dim con As SqlConnection = New SqlConnection(strConnString)
Dim sda As SqlDataAdapter = New SqlDataAdapter()
Dim cmd As SqlCommand = New SqlCommand("select distinct Country" & " from customers")
cmd.Connection = con
con.Open()
MaxCountry.DataSource = cmd.ExecuteReader()
MaxCountry.DataTextField = "Country"
MaxCountry.DataValueField = "Country"
MaxCountry.DataBind()
con.Close()
MaxCountry.Items.FindByValue(ViewState("Filter").ToString()).Selected = True
End Sub
Protected Sub CountryChanged(ByVal sender As Object, ByVal e As EventArgs)
Dim MaxCountry As DropDownList = CType(sender, DropDownList)
ViewState("Filter") = MaxCountry.SelectedValue
Me.BindGrid()
End Sub
Protected Sub OnPaging(ByVal sender As Object, ByVal e As GridViewPageEventArgs)
GridView1.PageIndex = e.NewPageIndex
Me.BindGrid()
End Sub
Screenshot