DataGird分页问题
加入了下面这段代码
private void dataGrid1_PageIndexChanged(object source, System.Web.UI.WebControls.DataGridPageChangedEventArgs e)
{
dataGrid1.CurrentPageIndex = e.NewPageIndex;
dataGrid1.DataBind();
}
浏览时还是只显示分页,点击无效果,不知道是怎么回事?
------解决方案--------------------如果在Page_Load里绑定了,最好判断回传
if(!IsPostBack)
{
}
------解决方案--------------------private void Page_Load(object sender, System.EventArgs e)
{
if (! IsPostBack )
{
//这里加上你的绑定方法
}
}
private void dataGrid1_PageIndexChanged(object source, System.Web.UI.WebControls.DataGridPageChangedEventArgs e)
{
dataGrid1.CurrentPageIndex = e.NewPageIndex;
//这里加上你的绑定方法
}
------解决方案--------------------SqlConnection conn = new SqlConnection(ConfigurationSettings.AppSettings[ "sqlConnection "]);
conn.Open();
string readmsgSql = "SQL语句 ";
SqlDataAdapter readmsgdap = new SqlDataAdapter(readmsgSql,conn);
DataSet Msgs = new DataSet();
readmsgdap.Fill(Msgs, "Messages ");
this.dataGrid1.DataSource = Msgs.Tables[0];
this.dataGrid1.DataBind();
把这些代码中写到一个方法中,绑定的时候才调用
------解决方案--------------------1,首先也第一次刷新时要绑定。
2,dataGrid1 的PageIndexChanged 要指向 void dataGrid1_PageIndexChanged
3,dataGrid1.CurrentPageIndex变为新页后要重新绑定
以下为简单的代码
protected void Page_Load(object sender, EventArgs e)
{
if(!Page.IsPostBack)
{
this.Bind
}
}
private void dataGrid1_PageIndexChanged(object source, System.Web.UI.WebControls.DataGridPageChangedEventArgs e)
{
dataGrid1.CurrentPageIndex = e.NewPageIndex;
this.Bind()
}
private void Bind() {
SqlConnection conn = new SqlConnection(ConfigurationSettings.AppSettings[ "sqlConnection "]);
conn.Open();
string readmsgSql = "SQL语句 ";
SqlDataAdapter readmsgdap = new SqlDataAdapter(readmsgSql, conn);
DataSet Msgs = new DataSet();
readmsgdap.Fill(Msgs, "Messages ");
this.dataGrid1.DataSource = Msgs.Tables[0];
this.dataGrid1.DataBind();
}