public partial class WebForm2 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
string connectionString =
WebConfigurationManager.ConnectionStrings["base"].ConnectionString;
string selectSQL = "SELECT author,book FROM ListItem";
SqlConnection con = new SqlConnection(connectionString);
SqlCommand cmd = new SqlCommand(selectSQL, con);
SqlDataAdapter adapter = new SqlDataAdapter(cmd);
DataSet ds = new DataSet();
adapter.Fill(ds, "ListItem");
grid.DataSource = ds;
grid.DataBind();
}
protected void grid_PageIndexChanged(object sender, EventArgs e)
{
grid.PageIndex=e.NewPageIndex;//have a error grid.DataBind();
}
}
答案 0 :(得分:6)
您处理了错误的事件,将其更改为PageIndexChanging
。 (你需要在aspx代码中将它连接起来)
protected void grid_PageIndexChanging(object sender, GridViewPageEventArgs e)
{
grid.PageIndex = e.NewPageIndex;
grid.DataBind();
}
您需要使用if(!Page.IsPostback){//do binding }