日期:2014-05-18  浏览次数:20442 次

关于gridview的分页的问题,望各位指点
我用的是gridview的自动分页,显示正常,按下一页也能看到下一页的东西,但是我要在下一页某一行操作的时候,他就直接返回第一页的那一行操作了,为什么,要怎么改呀,我刚接触asp.net,不是很清楚,是不是要重新绑定数据什么的,要怎么绑定,希望大家指点一下,谢谢:-)。


------解决方案--------------------
在事件里面设置GridView的PageIndex值为当前页
------解决方案--------------------
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;

public partial class nweslist : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
GridViewBind("");

}
private void GridViewBind(string Sqlsort)
{
string strconn = ConfigurationSettings.AppSettings["connstr"];
DataSet ds = new DataSet();
try
{
SqlConnection conn = new SqlConnection(strconn);
if (conn.State.ToString() == "Closed") 
conn.Open();
SqlCommand cmd = new SqlCommand();
cmd.CommandText = "select * from news_building order by number desc ";
cmd.Connection = conn;
SqlDataAdapter da = new SqlDataAdapter();
da.SelectCommand = cmd;
da.Fill(ds);
if (conn.State.ToString() == "Open") 
conn.Close();

GridView1.DataSource = ds.Tables[0].DefaultView;
GridView1.DataBind();


if (ds.Tables[0].Rows.Count == 0)
{
btnFirst.Visible = false;
btnPrev.Visible = false;
btnNext.Visible = false;
btnLast.Visible = false;

LblCurrentIndex.Visible = false;
LblPageCount.Visible = false;
}
else if (GridView1.PageCount == 1)
{
btnFirst.Visible = false;
btnPrev.Visible = false;
btnNext.Visible = false;
btnLast.Visible = false;
}

// 计算生成分页页码,分别为:"首 页" "上一页" "下一页" "尾 页"
btnFirst.CommandName = "1";
btnPrev.CommandName = (GridView1.PageIndex == 0 ? "1" : GridView1.PageIndex.ToString());
btnNext.CommandName = (GridView1.PageCount == 1 ? GridView1.PageCount.ToString() : (GridView1.PageIndex + 2).ToString());
btnLast.CommandName = GridView1.PageCount.ToString();
//
}
catch (Exception ex)
{
Response.Write("数据库错误,错误原因:" + ex.Message);
Response.End();
}
}
protected void PagerButtonClick(object sender, EventArgs e)
{
GridView1.PageIndex = Convert.ToInt32(((LinkButton)sender).CommandName) - 1;
GridViewBind("");
}
protected void showpage()
{
LblCurrentIndex.Text = "第 " + (GridView1.PageIndex + 1).ToString() + " 页";
LblPageCount.Text = "共 " + GridView1.PageCount.ToString() + " 页";
}
protected void CustomersGridView_PageIndexChanging(Object sender, GridViewPageEventArgs e)
{
GridView1.PageIndex = e.NewPageIndex;
GridView1.DataBind();
showpage();
}
}

------解决方案--------------------
就按照1楼说的在pageIndexChanging里面添加就可以了