1. 在GridView 的屬性裏增加分頁響應事件: OnPageIndexChanging="GridView1_PageIndexChanging" AllowPaging="True"
如:
<asp:GridView ID="GridView1" Width="1400" runat="server" AutoGenerateColumns="False" GridLines="None" PageSize="5" EmptyDataText="No Data" OnPageIndexChanging="GridView1_PageIndexChanging" AllowPaging="True">
2. 調整妳的代碼,妳現在的代碼是不支持分頁的,需要進行調整。
最簡單辦法是:在後臺增加緩存數據的變量將從數據庫中查詢到的數據保存到Session中,然後在進行數據綁定時調用此Session緩存的數據
簡單的實例:
/// <summary>
/// 緩存數據集
/// </summary>
protected DataSet CacheDataSet
{
get
{
if (null == Session["DataSet"])
{
Session["DataSet"] = new DataSet();
}
return (DataSet)Session["DataSet"];
}
set
{
Session["DataSet"] = value;
}
}
/// <summary>
/// 獲取數據庫數據
/// </summary>
private void getDate(string searchCondition)
{
string strSQL = "Select Sid,Rid,Bid,Bname,Borrowdate,returndate from State where Bname like '%" + searchCondition + "%'";
OracleCommand myComm = new OracleCommand(strSQL, conn);
conn.Open();
OracleDataAdapter adapter = new OracleDataAdapter(myComm);
DataSet dataset = new DataSet();
adapter.Fill(dataset);
CacheDataSet = dataset;
conn.Close();
}
/// <summary>
/// 表格數據分頁顯示
/// </summary>
private void doSearch(int pageIndex)
{
try
{
GridView1.PageIndex = pageIndex;
GridView1.DataSource = CacheDataSet;
GridView1.DataBind();
}
catch (Exception)
{
GridView1.EmptyDataText = "Database ERROR";
GridView1.DataSource = null;
GridView1.DataBind();
GridView1.EmptyDataText = "No Data";
}
}
/// <summary>
/// 處理分頁事件
/// </summary>
protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
{
doSearch(e.NewPageIndex);
}
// 進行調整後的原來的代碼
if (DropDownList1.SelectedIndex == 1)
{
getDate(TextBox1.Text);
doSearch(0);
}