在 ASP.NET WebForms 开发中,DataList 控件提供了极高的模板自定义能力,但也需要手动处理分页逻辑。下面是一个完整的页面类示例,展示了如何通过 PagedDataSource 实现数据分页,以及如何处理全选和命令事件。
using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
public partial class pages_DataList : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
BindData();
}
}
// 数据绑定核心逻辑
public void BindData()
{
DataTable dt = new DataTable("blog");
DataColumn dc1 = new DataColumn("id", typeof(int));
dc1.AutoIncrement = true;
dt.Columns.Add(dc1);
DataColumn dc2 = new DataColumn("blogname", typeof(string));
dt.Columns.Add(dc2);
DataColumn dc3 = new DataColumn("blogurl", ());
dt.Columns.Add(dc3);
DataColumn dc4 = DataColumn(, ());
dt.Columns.Add(dc4);
DataRow dr1 = dt.NewRow();
dr1[] = ;
dr1[] = ;
dr1[] = ;
dt.Rows.Add(dr1);
DataRow dr2 = dt.NewRow();
dr2[] = ;
dr2[] = ;
dr2[] = ;
dt.Rows.Add(dr2);
DataRow dr3 = dt.NewRow();
dr3[] = ;
dr3[] = ;
dr3[] = ;
dt.Rows.Add(dr3);
DataRow dr4 = dt.NewRow();
dr4[] = ;
dr4[] = ;
dr4[] = ;
dt.Rows.Add(dr4);
PagedDataSource pds = PagedDataSource();
pds.DataSource = dt;
pds.AllowPaging = ;
pds.PageSize = ;
(lblPresentPage.Text != )
{
pds.CurrentPageIndex = Convert.ToInt32(lblPresentPage.Text);
}
.DataList1.DataSource = pds;
.DataBind();
}
{
(.btnCheckOrCancle.Text == )
{
(DataListItem dl .DataList1.Items)
{
CheckBox cb = (CheckBox)dl.FindControl();
(cb != ) cb.Checked = ;
}
.btnCheckOrCancle.Text = ;
}
{
(DataListItem dl .DataList1.Items)
{
CheckBox cb = (CheckBox)dl.FindControl();
(cb != ) cb.Checked = ;
}
.btnCheckOrCancle.Text = ;
}
}
{
(e.CommandSource.GetType() == (LinkButton))
{
(((LinkButton)e.CommandSource).CommandName == )
{
Response.Write();
}
}
}
{
lblPresentPage.Text = ;
BindData();
}
{
index = Convert.ToInt32(lblPresentPage.Text);
(index > )
{
lblPresentPage.Text = (index - ).ToString();
BindData();
}
}
{
index = Convert.ToInt32(lblPresentPage.Text);
lblPresentPage.Text = (index + ).ToString();
BindData();
}
}

