Repeater控件的简单使用

本文介绍如何使用ASP.NET中的Repeater控件实现数据分页展示。通过结合PagedDataSource组件,实现了一个带有导航控件的分页功能,并通过数据绑定将省市区数据展示出来。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Repeater控件的简单使用

aspx页面:

ContractedBlock.gifExpandedBlockStart.gifCode
<asp:Repeater ID="Repeater1" runat="server" OnItemDataBound="Repeater1_ItemDataBound">
        
<HeaderTemplate>
        
<table width="500">
        
<tr style="background-color: #ccffcc;">
ExpandedBlockStart.gifContractedBlock.gif            
<td><%--城市名称--%></td>
ExpandedBlockStart.gifContractedBlock.gif            
<td><%--所属地市--%></td>
        
</tr>
        
</HeaderTemplate>
        
<ItemTemplate>
ExpandedBlockStart.gifContractedBlock.gif        
<tr><td><%#Eval("province"%></td><td><asp:Repeater ID="rep2" runat="server" DataSource='<%# Eval("sqlCity") %>'>
        
<ItemTemplate>
ExpandedBlockStart.gifContractedBlock.gif            
<%Eval("[\"city\"]")%><br>     
        
</ItemTemplate></asp:Repeater></td></tr>        
        
</ItemTemplate> 
        
<SeparatorTemplate>
        
<tr><td colspan="2"><hr size="1pt" /></td></tr>
        
</SeparatorTemplate>
ExpandedBlockStart.gifContractedBlock.gif        
<FooterTemplate><%--页脚模板--%>
        
<tr>
        
<td colspan="2" style="font-size: 12pt; color: #0099ff; background-color: #e6feda;">共 <asp:Label ID="lblpc" runat="server" Text="Label"></asp:Label> 页 当前为第 <asp:Label ID="lblp" runat="server" Text="Label"></asp:Label> 页 
            
<asp:HyperLink ID="hlfir" runat="server" Text="首页"></asp:HyperLink> 
            
<asp:HyperLink ID="hlp" runat="server" Text="上一页"></asp:HyperLink>
            
<asp:HyperLink ID="hln" runat="server" Text="下一页"></asp:HyperLink>
            
<asp:HyperLink ID="hlla" runat="server" Text="末页"></asp:HyperLink>
            
</td>
        
</tr>
        
</table>
        
</FooterTemplate>  
        
</asp:Repeater>

 

ascx页面:

ContractedBlock.gifExpandedBlockStart.gifCode
using System;
using System.Data;
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;
using System.Web.UI.WebControls;
using System.Data.SqlClient;

public partial class repeater : System.Web.UI.Page
{
    
protected void Page_Load(object sender, EventArgs e)
    {
        
if (!this.IsPostBack)
        {
            Repeater1.DataSource 
= pds();
            Repeater1.DataBind();
        }
    }
    
protected PagedDataSource pds()
    {
        
//创建一个数据库连接字符串
        string connstring = ConfigurationManager.ConnectionStrings["ceshiConnectionString"].ConnectionString;
        
//创建一个数据库连接
        SqlConnection con = new SqlConnection(connstring);
        
//
        SqlDataAdapter sda = new SqlDataAdapter("select * from province",con);
        
//
        DataSet ds = new DataSet();
        sda.Fill(ds,
"province");

        SqlDataAdapter sda2 
= new SqlDataAdapter("select * from city", con);
        sda2.Fill(ds, 
"city");

        ds.Relations.Add(
"sqlCity", ds.Tables["province"].Columns["provinceID"], ds.Tables["city"].Columns["father"]);

        PagedDataSource pds 
= new PagedDataSource();
        pds.DataSource 
= ds.Tables["province"].DefaultView;
        pds.AllowPaging 
= true;
        pds.PageSize 
= 5;
        pds.CurrentPageIndex 
= Convert.ToInt32(Request.QueryString["page"]);
        
return pds;
    }
    
protected void Repeater1_ItemDataBound(object sender, RepeaterItemEventArgs e)
    {
        
if (e.Item.ItemType == ListItemType.Footer)
        {
//判断当前项是页脚模板
            int n = pds().PageCount;//将分页总数赋给变量n
            int i = pds().CurrentPageIndex;//将当前分页码赋给i

            Label lblpc 
= (Label)e.Item.FindControl("lblpc");
            lblpc.Text 
= n.ToString();
            
//找到lblpc这个Label,将总页码赋给他
            Label lblp = (Label)e.Item.FindControl("lblp");
            lblp.Text 
= Convert.ToString(pds().CurrentPageIndex + 1);
            
//找到lblp这个Label,将当前页码赋给他,但是注意,因为页码从0开始,这里要直观的话就得加1
            HyperLink hlfir = (HyperLink)e.Item.FindControl("hlfir");
            hlfir.NavigateUrl 
= "?page=0";
            HyperLink hlla 
= (HyperLink)e.Item.FindControl("hlla");
            hlla.NavigateUrl 
= "?page=" + Convert.ToInt32(n - 1);
            
//找到表示最前页和末页的Label,为他们的NavigateUrl属性赋为第0页和最大页码减1
            HyperLink hlp = (HyperLink)e.Item.FindControl("hlp");
            HyperLink hln 
= (HyperLink)e.Item.FindControl("hln");
            
//找到表示上页和下页这两个控件
            if (i <= 0)
            {
//如果当前页已经是第0页
                hlp.Enabled = false;
                hlfir.Enabled 
= false;
                hln.Enabled 
= true;
            }
            
else
            {
                hlp.NavigateUrl 
= "?page=" + Convert.ToInt32(i - 1);
            }
            
if (i > n - 2)
            {
//如果当前项已经是最末页
                hln.Enabled = false;
                hlla.Enabled 
= false;
                hlp.Enabled 
= true;
            }
            
else
            {
                hln.NavigateUrl 
= "?page=" + Convert.ToInt32(i + 1);
            }
        }
    }
}

转载于:https://www.cnblogs.com/ytwsz/archive/2009/05/16/1458129.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值