双11狂欢大促

盲盒赢 定制羽绒服

开启惊喜盲盒,抽取你的专属好礼

— 惊喜奖品抢先看 —

DataList分页问题

zxd9208 zxd9208 墨衍会员 · AI 创作全网分发 墨衍智能分发 通过墨衍一键同步至各平台 可同步平台 CSDN微信公众号微博知乎掘金百家号博客园抖音小红书 你的文章也可以这样分发 墨衍会员 ¥399起/年,写一次发全网 我也要 + 加入社区
上代码:数据绑定成功了 就是一直都是显示第一页内容 “首页 上一页 下一页 末页 跳转 ”功能都没有一个实现的 这是为什么求高手看看


 private string myConn = ConfigurationManager.ConnectionStrings["ClientsConnectionString"].ConnectionString;
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
Bind(0);
}

//创建一个分页数据源的对象且一定要声明为静态
protected static PagedDataSource ps = new PagedDataSource();
private void Bind(int CurrentPage)
{
string selectSql = "select ClientID,ClientName,AddressStr,Email from OrderClient where ClientID<=700";

SqlConnection connectiong = new SqlConnection(myConn);
SqlDataAdapter da = new SqlDataAdapter(selectSql, connectiong);

DataSet ds = new DataSet();
da.Fill(ds, "OrderClient");

DataList1.DataSource = ds;
DataList1.DataBind();
ps.DataSource = ds.Tables["OrderClient"].DefaultView;
ps.AllowPaging = true;//是否可以分页
ps.PageSize = 4;
ps.CurrentPageIndex = CurrentPage;//取得当前页码

DataList1.DataSource = ps;
DataList1.DataKeyField = "ClientID";
DataList1.DataBind();
}


protected void DataList1_ItemCommand(object source, DataListCommandEventArgs e)
{
switch (e.CommandName)
{
//以下5种情况分别为捕获用户点击第一页、上一页、下一页、最后一页和页面跳转是发生的事件
case "first":
ps.CurrentPageIndex = 0;
Bind(ps.CurrentPageIndex);
break;
case "pre":
ps.CurrentPageIndex = ps.CurrentPageIndex - 1;
Bind(ps.CurrentPageIndex);
break;
case "next":
ps.CurrentPageIndex = ps.CurrentPageIndex + 1;
Bind(ps.CurrentPageIndex);
break;
case "last":
ps.CurrentPageIndex = ps.PageCount - 1;
Bind(ps.CurrentPageIndex);
break;
case "search":
if (e.Item.ItemType == ListItemType.Footer)
{
int PageCount = int.Parse(ps.PageCount.ToString());
TextBox txtPage = e.Item.FindControl("txtPage") as TextBox;
int myPageNum = 0;
if (!txtPage.Text.Equals(""))
{
myPageNum = Convert.ToInt32(txtPage.Text.ToString());
if (myPageNum <= 0 || myPageNum > PageCount)
Response.Write("<script>alert('输入页数没有超出页数总数')</script>");
else

Bind(myPageNum - 1);
}

}
break;

}
}
protected void DataList1_ItemDataBound(object sender, DataListItemEventArgs e)
{
if (e.Item.ItemType == ListItemType.Footer)
{
//得到脚模板中的控件。并创建变量
Label CurrentPage = e.Item.FindControl("lblNowPage") as Label;
Label PageCount = e.Item.FindControl("lblCount") as Label;
LinkButton FirstPage = e.Item.FindControl("lnkbtnFirst") as LinkButton;
LinkButton PrePage = e.Item.FindControl("lnkbtnFont") as LinkButton;
LinkButton NextPage = e.Item.FindControl("lnkbtnNext") as LinkButton;
LinkButton LastPage = e.Item.FindControl("lnkbtnLast") as LinkButton;
CurrentPage.Text = (ps.CurrentPageIndex + 1).ToString();//绑定显示当前页
PageCount.Text = ps.PageCount.ToString();//绑定显示总页数
if (ps.IsFirstPage)
{
FirstPage.Enabled = false;
PrePage.Enabled = false;
}
if (ps.IsLastPage)
{
NextPage.Enabled = false;
LastPage.Enabled = false;
}
}
}
...全文
评论 12
收藏 已收藏
分享
返回顶部
zxd9208
.NET社区 62,274 成员 668,968 内容