• 博客园logo
  • 会员
  • 众包
  • 新闻
  • 博问
  • 闪存
  • 赞助商
  • HarmonyOS
  • Chat2DB
    • 搜索
      所有博客
    • 搜索
      当前博客
  • 写随笔 我的博客 短消息 简洁模式
    用户头像
    我的博客 我的园子 账号设置 会员中心 简洁模式 ... 退出登录
    注册 登录
June's New World
知识和经验都是一点点积累的!现在努力也不晚,June加油!
博客园    首页    新随笔    联系   管理    订阅  订阅
DataList的使用+分页

datalist和repeater一样,都是根据自己的需要添加自己的模板,简单例子如下:

前台代码如下:

<body>
    <form id="form1" runat="server">
    <div>
        <asp:DataList ID="DataList1" runat="server" DataKeyField="ID"  RepeatDirection="Horizontal">
            <ItemTemplate>
                <div style="text-align: left; background-color: #99ffcc;">
                    ID:<asp:Label ID="IDLabel" runat="server" Text='<%# Eval("ID") %>'></asp:Label><br />
                    Name:<asp:Label ID="Pro_NameLabel" runat="server" Text='<%# Eval("Name") %>'></asp:Label><br />
                    Price:<asp:Label ID="Pro_PriceLabel" runat="server" Text='<%# Eval("Price") %>'></asp:Label><br />
                    Introduce:<asp:Label ID="Pro_IntroduceLabel" runat="server" Text='<%# Eval("Introduce") %>'></asp:Label><br />
                </div>                           
            </ItemTemplate>
        </asp:DataList>
        当前页:<asp:Label ID="lblCurrent" runat="server" Text="1"></asp:Label>
        总页数:<asp:Label ID="lblTotal" runat="server" Text="Label"></asp:Label>
        <asp:LinkButton ID="lbtnFirst" runat="server" OnClick="lbtnFirst_Click">第一页</asp:LinkButton>
        <asp:LinkButton ID="lbntUp" runat="server" OnClick="lbntUp_Click">上一页</asp:LinkButton>
        <asp:LinkButton ID="lbtnDown" runat="server" OnClick="lbtnDown_Click">下一页</asp:LinkButton>
        <asp:LinkButton ID="lbtnLast" runat="server" OnClick="lbtnLast_Click">最后一页</asp:LinkButton>
    </div>
    </form>
</body>

后台代码如下:

    protected void Page_Load(object sender, EventArgs e)
    {
        DataListBind();
    }

    private void DataListBind()
    {
        int current_page = Convert.ToInt32(lblCurrent.Text);
        SqlConnection con = new SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString);
        SqlDataAdapter oda = new SqlDataAdapter("select * from June_Pro", con);
        DataSet ds = new DataSet();
        oda.Fill(ds);

        PagedDataSource ps = new PagedDataSource();
        ps.DataSource = ds.Tables[0].DefaultView;
        ps.AllowPaging = true;
        ps.PageSize = 4;
        lblTotal.Text = ps.PageCount.ToString();
        ps.CurrentPageIndex = current_page - 1;
        lbtnFirst.Enabled = true;
        lbntUp.Enabled = true;
        lbtnDown.Enabled = true;
        lbtnLast.Enabled = true;
        if (current_page == 1)
        {
            lbtnFirst.Enabled = false;
            lbntUp.Enabled = false;
        }
        if (current_page == Convert.ToInt32(lblTotal.Text))
        {
            lbtnLast.Enabled = false;
            lbtnDown.Enabled = false;
        }
        DataList1.DataSource = ps;
        DataList1.DataBind();
    }
    protected void lbtnFirst_Click(object sender, EventArgs e)
    {
        lblCurrent.Text = "1";
        DataListBind();
    }
    protected void lbtnDown_Click(object sender, EventArgs e)
    {
        lblCurrent.Text = (Convert.ToInt32(lblCurrent.Text) + 1).ToString();
        DataListBind();
    }
    protected void lbntUp_Click(object sender, EventArgs e)
    {
        lblCurrent.Text = (Convert.ToInt32(lblCurrent.Text) - 1).ToString();
        DataListBind();
    }
    protected void lbtnLast_Click(object sender, EventArgs e)
    {
        lblCurrent.Text = lblTotal.Text;
        DataListBind();
    }

运行结果如下图:

posted on 2011-03-27 16:41  冬雨在路上  阅读(10074)  评论(2)    收藏  举报
刷新页面返回顶部
博客园  ©  2004-2025
浙公网安备 33010602011771号 浙ICP备2021040463号-3