asp.net

DataList用法之小博客

2011-03-19
<body>
    <form id="form1" runat="server">
    <div>
    <center>
        <asp:DataList ID="DataList1" runat="server" Width="554px" OnItemCommand="DataList1_ItemCommand">
        <HeaderTemplate><table width="100%">
        <tr><td>选择</td><td>图象</td><td>博客名</td><td>博客地址</td><td>操作</td></tr>
        </HeaderTemplate>
        <ItemTemplate>
        <tr>
            <td><asp:CheckBox ID="select" runat="server" /></td>
            <td><asp:Image ID="image" runat="server"  Height="80" Width="80" ImageUrl="~/image/Blue hills.jpg"/></td>
            <td><asp:Label ID="blogname" runat="server"  Text= <%# DataBinder.Eval(Container.DataItem,"blogname") %> ></asp:Label></td>
            <td><asp:Label ID="blogurl" runat="server" Text= <%# DataBinder.Eval(Container.DataItem,"blogurl") %> /></td>
            <td><asp:LinkButton ID="accept" runat="server" CommandName="accept" Text="接受"   /> <asp:LinkButton ID="reguest" runat="server" Text="拒绝"/></td>
        </tr>
        </ItemTemplate>
        <SeparatorTemplate><tr><td colspan="5"><hr /></td></tr></SeparatorTemplate>
        <FooterTemplate></table></FooterTemplate>
        </asp:DataList>&nbsp;</center>
        &nbsp; &nbsp; &nbsp;&nbsp;
       
            <asp:Button ID="Button1" runat="server" Text="全选" OnClick="Button1_Click" />&nbsp;<br />
        &nbsp; &nbsp; &nbsp;
        <asp:Label ID="Label1" runat="server" Text="当前页:"></asp:Label>
        <asp:Label ID="Label2" runat="server" Text="1" Width="31px"></asp:Label>
        <asp:Label ID="Label3" runat="server" Text="总页数:"></asp:Label>
        <asp:Label ID="Label4" runat="server" Width="58px"></asp:Label>
        &nbsp; &nbsp;&nbsp;
        <asp:LinkButton ID="LinkButton1" runat="server" OnClick="LinkButton1_Click">首页</asp:LinkButton>
        <asp:LinkButton ID="LinkButton2" runat="server" OnClick="LinkButton2_Click">上一页</asp:LinkButton>
        <asp:LinkButton ID="LinkButton3" runat="server" OnClick="LinkButton3_Click">下一页</asp:LinkButton>
        <asp:LinkButton ID="LinkButton4" runat="server" OnClick="LinkButton4_Click">末页</asp:LinkButton></div> 
    </form>
</body>


隐藏代码如下:
 

Title

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的用法的一个实例,主要实现了博客的功能