1

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.

Amarnath Balasubramanian
  • 9,300
  • 8
  • 34
  • 62
sbs
  • 4,102
  • 5
  • 40
  • 54

5 Answers5

2

Add the NewPageIndex code in the PageIndexChanging event:

managerList.PageIndex = e.NewPageIndex;
bindgrid();
Jayesh
  • 1,511
  • 1
  • 16
  • 37
1

Below might help you

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

}
Saurabh
  • 5,661
  • 2
  • 26
  • 32
  • no it won't. when i click on "next page", it gives me an empty list. – sbs Jun 30 '11 at 15:48
  • protected void managerList_PageIndexChanging(object sender, GridViewPageEventArgs e) { managerList.PageIndex = e.NewPageIndex; bindgrid(); } – Saurabh Jun 30 '11 at 16:21
1

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.

Off The Gold
  • 1,228
  • 15
  • 28
Muhammad Akhtar
  • 51,913
  • 37
  • 138
  • 191
0

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();
}
Bit Manipulator
  • 248
  • 1
  • 2
  • 17
0

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.

Jay
  • 6,224
  • 4
  • 20
  • 23