當前位置:吉日网官网 - 油畫收藏 - 查詢結果用gridview分頁,點擊下壹頁報錯,求大神幫忙,不要百度糊弄我

查詢結果用gridview分頁,點擊下壹頁報錯,求大神幫忙,不要百度糊弄我

妳需要做如下改進才能達到設置了查詢條件,查詢出結果後還能進行下壹頁的切換:

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);

}

  • 上一篇:李清照的歷史典故
  • 下一篇:夢見前夫要結婚了,買糖紙。
  • copyright 2024吉日网官网