隐藏代码如下:
using System; using System.Data; using System.Data.SqlClient; using System.Configuration; using System.Collections; using System.Web; using System.Web.Security; using System.Web.UI; using System.Web.UI.WebControls; using System.Web.UI.WebControls.WebParts; using System.Web.UI.HtmlControls;
public partial class 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 dc = new DataColumn("id", typeof(int)); //定义一个字段对象 dc.AutoIncrement = true; //将id字段设为自动增长 dt.Columns.Add(dc); //添加第一个字段
dc = new DataColumn("blogname",typeof(string)); dt.Columns.Add(dc);
dc = new DataColumn("blogurl",typeof(string)); dt.Columns.Add(dc); /* 以上总结为,定义一个表*/
DataRow dr = dt.NewRow(); //定义一个DataRow对象 dr["blogname"] = "Tom s blog"; dr["blogurl"] = "http://www.sinablog.com"; dt.Rows.Add(dr);
dr = dt.NewRow(); dr["blogname"] = "Rose s blog"; dr["blogurl"] = "http://www.cnblog.com"; dt.Rows.Add(dr);
dr=dt.NewRow(); dr["blogname"]="Willy s blog"; dr["blogurl"]="http://www.ddblog.com"; dt.Rows.Add(dr);
dr=dt.NewRow(); dr["blogname"]="hugo s blog"; dr["blogurl"]="http://www.hublog.com"; dt.Rows.Add(dr);
dr=dt.NewRow(); dr["blogname"]="bosco s blog"; dr["blogurl"]="http://www.boblog.com"; dt.Rows.Add(dr); /* 为表添加数据 */ PagedDataSource pds = new PagedDataSource(); pds.DataSource = dt.DefaultView ; //帮分页配置数据源 pds.AllowPaging = true; //允许分页 pds.PageSize = 2; //设置两个数据行一页 int CurrentIndex = Convert.ToInt32(this.Label2.Text) - 1; //当前页索引=当前页值-1 pds.CurrentPageIndex = CurrentIndex; //当前页索引 if(CurrentIndex==0 ) //首页时的状态 { this.LinkButton1.Enabled=false; this.LinkButton2.Enabled=false; this.LinkButton3.Enabled=true; this.LinkButton4.Enabled=true; } if(CurrentIndex==pds.PageCount-1) //末页时的状态 { this.LinkButton1.Enabled=true; this.LinkButton2.Enabled=true; this.LinkButton3.Enabled=false; this.LinkButton4.Enabled=false;
} Label4.Text = pds.PageCount.ToString(); //总页数
this.DataList1.DataSource = pds; //获取数据源 this.DataList1.DataBind();
} protected void DataList1_ItemCommand(object source, DataListCommandEventArgs e) //通过点击事件获取你要的信息 { if ( e.CommandSource.GetType() == typeof(LinkButton)) //先判断命令源的类型是哪种控件类型 { if ( ((LinkButton)e.CommandSource).CommandName == "accept") //由于有几个LinkButton,所以要判断是哪个LinkButton { Response.Write(((Label)e.Item.FindControl("blogname")).Text); //打应相应控件对应的内容 } } } protected void Button1_Click(object sender, EventArgs e) { if (this.Button1.Text == "全选") { foreach (DataListItem dli in DataList1.Items) //遍历DataList1的全部项 { CheckBox cb = (CheckBox)dli.FindControl("select"); //查找ID为select的CheckBox控件 cb.Checked = true; } Button1.Text = "全部取消"; } else { foreach (DataListItem dli in DataList1.Items) { CheckBox cb = (CheckBox)dli.FindControl("select"); cb.Checked = false; } Button1.Text = "全选"; } } protected void LinkButton1_Click(object sender, EventArgs e) { this.Label2.Text = "1"; //首页 bindData(); } protected void LinkButton2_Click(object sender, EventArgs e) { int temp = Convert.ToInt32(this.Label2.Text) - 1; //上一页 Label2.Text = temp.ToString(); bindData(); } protected void LinkButton3_Click(object sender, EventArgs e) { int temp = Convert.ToInt32(this.Label2.Text) + 1; //下一页 Label2.Text = temp.ToString(); bindData(); }
protected void LinkButton4_Click(object sender, EventArgs e) { this.Label2.Text = this.Label4.Text; //末页 bindData(); } }
界面如下 以上是我试验DataList的用法的一个实例,主要实现了博客的功能