sbs
sbs

Reputation: 4152

asp.net gridview dynamically binding

i have a GridView,

<asp:GridView ID="managerList" runat="server" DataSourceID="SqlDataSource2">

in the code behind,

protected void Page_Load(object sender, EventArgs e)
{
    SqlDataSource2.SelectCommand = "select * from manager";
    managerList.AllowPaging = true;
}

when i load the page, it works fine, the paging works fine, too.

Then i want to get the subset of the list by click on a search button:

protected void btnSearch_Click(object sender, EventArgs e)
{
    SqlDataSource2.SelectCommand = "select * from manager where age > 30";
    managerList.DataBind();
}

it works fine, give me the subset of the list.

However, when i click on "next page", it gives me the whole list, page #2. I know it's because it sends a postback, and it bind the original select command. But how can i do to give me the subset of the list when i click on "next page"?

Thank you!

UPDATES: if i change the code into this:

protected void Page_Load(object sender, EventArgs e)
{
  if(!IsPostBack)
  {
    SqlDataSource2.SelectCommand = "select * from manager";
    managerList.AllowPaging = true;
  }
}

it gives me an empty list when i click on "next page".

it might be tempted to add IsPostBack, but this not work.

Upvotes: 1

Views: 4422

Answers (5)

Muhammad Akhtar
Muhammad Akhtar

Reputation: 52241

You need to put your code under !IsPostBack() in the page_load event. like...

protected void Page_Load(object sender, EventArgs e)
{
  if(!IsPostBack)
  {
    SqlDataSource2.SelectCommand = "select * from manager";
    managerList.AllowPaging = true;
  }
}

Reason: Whenever you hit the Next button, your page load event is called before the PageIndexChanging Event handler of Gridview.

Upvotes: 1

Bit Manipulator
Bit Manipulator

Reputation: 358

Store the most recent SQL Query in a global static string and then use the following code.

static String previousSQL_Query;

protected void Page_Load(object sender, EventArgs e)
{
  if(IsPostBack)
  {
    SqlDataSource2.SelectCommand = previousSQL_Query;
  }
  else
  {
       SqlDataSource2.SelectCommand = "select * from manager";
       managerList.AllowPaging = true;
  }
}

protected void btnSearch_Click(object sender, EventArgs e)
{
    SqlDataSource2.SelectCommand = "select * from manager where age > 30";
    previousSQL_Query = SqlDataSource2.SelectCommand;
    managerList.DataBind();
}

Upvotes: 0

Jayesh
Jayesh

Reputation: 1523

Add the NewPageIndex code in the PageIndexChanging event:

managerList.PageIndex = e.NewPageIndex;
bindgrid();

Upvotes: 2

Saurabh
Saurabh

Reputation: 5727

Below might help you

protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
    {
    SqlDataSource2.SelectCommand = "select * from manager";
    managerList.AllowPaging = true;
    }

}

Upvotes: 1

Jay
Jay

Reputation: 6294

Page_Load fires every time the page is loaded, including postbacks, so your select statement is getting reset. Try setting a viewstate value to keep your select statement.

Upvotes: 0

Related Questions