纠结。aspnetpager和DataList绑定了以后就只有出现一页
这学期刚学了ASP.net一本书什么都没教,只是爱好在家做,但是在网上找了很多资料结果aspnetpager和DateList绑定了可是只出现的一页数据,这是一个考试系统,正在做分页,但是如果不用DateList就是所有的单选题全都出现在浏览器很不和谐。一下是我在网上找的代码还是能用但是只能显示一页,纠结了一天了:
if (!IsPostBack)
{
SqlConnection constr = new SqlConnection("Data Source=.\\SQLEXPRESS;AttachDbFilename=|DataDirectory|\\Database.mdf;Integrated Security=True;User Instance=True");
constr.Open();
SqlCommand cmd = new SqlCommand();
cmd.Connection = constr;
cmd.CommandText = "select count(*) from QuestionBank_Single";
AspNetPager1.AlwaysShow = true;
AspNetPager1.PageSize = 3;
AspNetPager1.RecordCount = (int)cmd.ExecuteScalar();
constr.Close();
DateBind();
}
}
private void DateBind()
{
SqlConnection con = new SqlConnection("Data Source=.\\SQLEXPRESS;AttachDbFilename=|DataDirectory|\\Database.mdf;Integrated Security=True;User Instance=True");
SqlDataAdapter adpter = new SqlDataAdapter("select * from QuestionBank_Single",con);
DataSet ds=new DataSet();
adpter.Fill(ds,AspNetPager1.PageSize*(AspNetPager1.CurrentPageIndex-1),AspNetPager1.PageSize,"QuestionBank_Single");
DataList1.DataSource = ds.Tables["QuestionBank_Single"];
DataList1.DataBind();
}
protected void AspNetPager1_PageChanging(object src, Wuqi.Webdiyer.PageChangingEventArgs e)
{
AspNetPager1.CurrentPageIndex = e.NewPageIndex;
DateBind();
}
[解决办法]
http://blog.csdn.net/dalmeeme/article/details/6769583
这个是GridView的,改成DataList就行了,另外分页的SQL语句改一下,用双top那种。
[解决办法]
datalist分页代码如下:
1控件准备:2个Label 一个用来绑定第几页,一个用来绑定总页数;5个linkbutton 分别为(首页、上一页、下一页、尾页、Go);1个textbox用来绑定跳转到的页数2代码:首先在load里面声明刚开始的页数为1,(注意:一定要在非回传页面下) 即 //非回传,非常重要 if (!IsPostBack) { Lcount.Text = "1"; MoreBind(); } // 绑定数据: public void CheckBind() { Infos i = new Infos(); string tile = Ttile.Text; string content = Tcontent.Text; string puser = Tpuser.Text; //第几页 int CurPage= Convert.ToInt32(Lcount.Text); //当前页面从Page查询参数获取 PagedDataSource ps = new PagedDataSource(); ps.DataSource = i.QT_Select(tile, content, puser).DefaultView; ps.AllowPaging = true; ps.PageSize = 10; ps.CurrentPageIndex = CurPage - 1; //绑定总页数 Lsl.Text = ps.PageCount.ToString(); if (ps.IsFirstPage) { Lfrist.Enabled = false; Lpev.Enabled = false; } else { Lfrist.Enabled = true; Lpev.Enabled = true; } if (ps.IsLastPage) { Lnext.Enabled = false; Llast.Enabled = false; } else { Lnext.Enabled = true; Llast.Enabled = true; } DlMore.DataSource = ps; DlMore.DataBind(); }3 5个linkbutton 的单击事件: //首页 protected void Lfrist_Click(object sender, EventArgs e) { Lcount.Text = "1"; CheckBind(); } //上一页 protected void Lpev_Click(object sender, EventArgs e) { Lcount.Text = Convert.ToString(Convert.ToInt32(Lcount.Text) - 1); CheckBind(); } //下一页 protected void Lnext_Click(object sender, EventArgs e) { Lcount.Text = Convert.ToString(Convert.ToInt32(Lcount.Text) + 1); CheckBind(); } //尾页 protected void Llast_Click(object sender, EventArgs e) { Lcount.Text = Lsl.Text; CheckBind(); } //跳转 protected void LGo_Click(object sender, EventArgs e) { if (Convert.ToInt32(TGO.Text) >= 1 && Convert.ToInt32(TGO.Text) <= Convert.ToInt32(Lsl.Text)) { Lcount.Text = TGO.Text; } else { Response.Write("<script>alert('请输入正确页数!');</script>"); } CheckBind(); TGO.Text = ""; }