datalist分页实例

html代码:

 <form id="form1" runat="server">
    <div>
    
        <asp:DataList ID="DataList1" runat="server">
            <HeaderTemplate>
                <table cellpadding="0" cellspacing="0" class="style1">
                    <tr>
                        <td style="width:20%">
                            IP</td>
                        <td style="width:20%">
                            登录时间</td>
                        <td style="width:20%">
                            离开时间</td>
                        <td style="width:20%">
                            浏览器</td>
                        <td style="width:20%">
                            操作系统</td>
                    </tr>
                </table>
            </HeaderTemplate>
            <ItemTemplate>
                <table cellpadding="0" cellspacing="0" class="style1">
                    <tr>
                        <td style="width:20%">
                           <%# DataBinder.Eval(Container.DataItem,"IP") %></td>
                        <td style="width:20%">
                            <%# DataBinder.Eval(Container.DataItem,"LoginTime") %></td>
                        <td style="width:20%">
                           <%# DataBinder.Eval(Container.DataItem,"LeaveTime") %></td>
                           
                        <td style="width:20%">
                            <%# DataBinder.Eval(Container.DataItem,"Browser") %></td>
                        <td style="width:20%">
                           <%# DataBinder.Eval(Container.DataItem,"OS") %></td>
                    </tr>
                </table>
            </ItemTemplate>
        </asp:DataList>
    
    </div>
   共有<asp:Label ID="labcount" runat="server" Text=""></asp:Label>
    &nbsp;页&nbsp;&nbsp;&nbsp;当前
    <asp:Label ID="labnowpage" runat="server" Text="1"></asp:Label>
    页&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
    <asp:LinkButton ID="lkbtnfirst" runat="server" οnclick="lkbtnfirst_Click">首页</asp:LinkButton>
&nbsp;&nbsp;&nbsp;
    <asp:LinkButton ID="lkbtnfront" runat="server" οnclick="lkbtnfront_Click">上一页</asp:LinkButton>
&nbsp;&nbsp;&nbsp;
    <asp:LinkButton ID="lkbtnnext" runat="server" οnclick="lkbtnnext_Click">下一页</asp:LinkButton>
&nbsp;&nbsp;&nbsp;
    <asp:LinkButton ID="lkbtnlast" runat="server" οnclick="lkbtnlast_Click">尾页</asp:LinkButton>
    </form>

 

datalist分页是通过PagedDataSource类来实现的。该类封装数据绑定控件与分页相关的属性,以允许该控件执行分页操作。

后台代码

 

using System.data.sqlclient;

 DB db = new DB();
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            bind();
        }
    }
    public void bind()
    {
     
        int currentpage = Convert.ToInt32(labnowpage.Text);
        PagedDataSource ps = new PagedDataSource();//生成pageddatasource的实例
        string selstr = "select * from tb_CounterInfo";
        string slqcon = ConfigurationManager.AppSettings["getcon"].ToString();
        SqlConnection con = new SqlConnection(slqcon);
        con.Open();
        SqlDataAdapter da = new SqlDataAdapter(selstr,con);
        DataSet ds=new DataSet();
        da.Fill(ds, "CounterInfo");
        ps.DataSource = ds.Tables["CounterInfo"].DefaultView;
        ps.AllowPaging = true;
        ps.PageSize = 4;
        ps.CurrentPageIndex = currentpage - 1;
        lkbtnfirst.Enabled = true;
        lkbtnfront.Enabled = true;
        lkbtnnext.Enabled = true;
        lkbtnlast.Enabled = true;
        if (currentpage == 1)
        {
            lkbtnlast.Enabled = false;
            lkbtnfront.Enabled = false;
        }
        if (currentpage == ps.PageCount)
        {
            lkbtnnext.Enabled = false;
            lkbtnlast.Enabled = false;
        }
        this.labcount.Text = Convert.ToString(ps.PageCount);
        this.DataList1.DataSource = ps;
        this.DataList1.DataKeyField = "ID";
        this.DataList1.DataBind();
    }
    protected void lkbtnfirst_Click(object sender, EventArgs e)
    {
        this.labnowpage.Text = "1";

 

        bind();
    }
    protected void lkbtnfront_Click(object sender, EventArgs e)
    {
        this.labnowpage.Text = Convert.ToString(Convert.ToInt32(this.labnowpage.Text) - 1);
        bind();
    }
    protected void lkbtnnext_Click(object sender, EventArgs e)
    {
        this.labnowpage.Text = Convert.ToString(Convert.ToInt32(this.labnowpage.Text) + 1);
        bind();
    }
    protected void lkbtnlast_Click(object sender, EventArgs e)
    {
        this.labnowpage.Text = this.labcount.Text;
        bind();
    }

 

获取DataList控件中控件数据的方法通常使用两种方法:

一种是通过e.Item.Controls[0]索引直接访问Item中的控件,另一种方法是使用FindControl()方法来查找。

转载于:https://www.cnblogs.com/dearamy/archive/2010/04/18/1714998.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值