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

        }
    }
4

1 回答 1

6

您处理了错误的事件,将其更改为PageIndexChanging. (并且您需要将其连接到 aspx 代码中)

protected void grid_PageIndexChanging(object sender, GridViewPageEventArgs e)
{
   grid.PageIndex = e.NewPageIndex;
   grid.DataBind();
}

并且您需要在页面加载中围绕网格的绑定if(!Page.IsPostback){//do binding }

于 2013-06-07T12:57:48.620 回答