GridView'grid'触发了未处理的事件PageIndexChanging

时间:2013-06-07 12:52:15

标签: c# asp.net page-index-changed

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

        }
    }

1 个答案:

答案 0 :(得分:6)

您处理了错误的事件,将其更改为PageIndexChanging。 (你需要在aspx代码中将它连接起来)

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

您需要使用if(!Page.IsPostback){//do binding }

在页面加载中包围网格的绑定