您需要在處理PageIndexChanging事件的代碼後面聲明一個方法。
事情與此類似:
protected void GridView1_PageIndexChanging (object sender, GridViewPageEventArgs e)
{
GridView1.PageIndex = e.NewPageIndex;
bindGridView(); //bindgridview will get the data source and bind it again
}
private void bindGridView()
{
GridView1.DataSource=getData();
GridView1.DataBind();
}
提供了示例代碼:
protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
{
GridView1.PageIndex = e.NewPageIndex;
bindGridView(); //bindgridview will get the data source and bind it again
}
protected void Page_Load(object sender , EventArgs e)
{
if(!IsPostBack)
bindGridView();
}
//this is some sample data
private void bindGridView()
{
DataTable t = new DataTable();
t.Columns.Add("Col1");
t.Columns.Add("Col2");
DataRow r = null;
for (int i = 0; i < 25; i++)
{
r = t.NewRow();
r.ItemArray = new object[] { "Val" + i, " Another " + i };
t.Rows.Add(r);
}
GridView1.DataSource = t;
GridView1.DataBind();
}
這是標記:
<asp:GridView OnPageIndexChanging="GridView1_PageIndexChanging" AllowPaging="true" PageSize="10" ID="GridView1" runat="server" AutoGenerateColumns="true">
產生以下:
亞我做到了,但沒有效果。保護無效GridView1_PageIndexChanging(對象發件人,GridViewPageEventArgs E) { //串S1 = 「來自主選擇*」; //數據集DS = SqlHelper.GetDataSet(CommandType.Text,S1, 「主」); // DataTable table = ds.Tables [「master」]; //如果(table.Rows.Count> 0) // {// = GridView1.DataSource DS; // GridView1.DataBind(); //} GridView1.PageIndex = e.NewPageIndex; //綁定網格 GridView1.DataBind(); } 和aspx文件 –
@JuiTest這個代碼發佈到你的答案,而不是作爲一個評論。在那裏看到它會更有幫助。 – Icarus
我無法回答自己的問題。 –