using System;
using System.Data;
using System.Configuration;
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.Data.SqlClient;
![](/Images/OutliningIndicators/None.gif)
public partial class _Default : System.Web.UI.Page
![](/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](/Images/OutliningIndicators/ContractedBlock.gif)
![](https://www.cnblogs.com/Images/dot.gif)
{
protected void Page_Load(object sender, EventArgs e)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](https://www.cnblogs.com/Images/dot.gif)
{
if (!IsPostBack)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](https://www.cnblogs.com/Images/dot.gif)
{
myRepeater.DataSource = pds();
//为Repeater1控件指定数据源
myRepeater.DataBind();
//这个当然是必须的,绑定上去
}
}
![](/Images/OutliningIndicators/InBlock.gif)
private PagedDataSource pds()
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](https://www.cnblogs.com/Images/dot.gif)
{
SqlConnection myconn = new SqlConnection("Data Source=192.168.201.108;Initial CataLog=陈乐斌;User ID=sa;Password=yttvmedia");
myconn.Open();
SqlDataAdapter da = new SqlDataAdapter("select id,username,password,email,tel,age from [user] order by id desc", myconn);
DataSet ds = new DataSet();
da.Fill(ds, "user");
SqlDataAdapter da2 = new SqlDataAdapter("select classid,classname,teachername,id,time,score,slevel from [class]", myconn);
da2.Fill(ds, "class");
ds.Relations.Add("myrela", ds.Tables["user"].Columns["id"], ds.Tables["class"].Columns["id"]);
//为上面建立的两个表创建一个关系,指明父列和子列的名称并为他们的关系命名
![](/Images/OutliningIndicators/InBlock.gif)
PagedDataSource pds = new PagedDataSource();
//初始化一个PagedDataSource,允许控件分页
pds.DataSource = ds.Tables["user"].DefaultView;
//将上面的ds转换成标准数据视图
pds.AllowPaging = true;
//允许分页
pds.PageSize = 5;
//每页大小为5
pds.CurrentPageIndex = Convert.ToInt32(Request.QueryString["page"]);
//设置当前页
return pds;
//将处理完毕的pds对象发出去
}
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
protected void myRepeater_ItemDataBound(object sender, RepeaterItemEventArgs e)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](https://www.cnblogs.com/Images/dot.gif)
{
if (e.Item.ItemType == ListItemType.Footer)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](https://www.cnblogs.com/Images/dot.gif)
{//判断当前项是页脚模板
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");
//找到表示上页和下页这两个控件
![](/Images/OutliningIndicators/InBlock.gif)
if (i <= 0)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](https://www.cnblogs.com/Images/dot.gif)
{//如果当前页已经是第0页
hlfir.Enabled = false;
hlp.Enabled = false;
hln.Enabled = true;
}
else
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](https://www.cnblogs.com/Images/dot.gif)
{
hlp.NavigateUrl = "?page=" + Convert.ToInt32(i-1);
}
![](/Images/OutliningIndicators/InBlock.gif)
if (i > n - 2)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](https://www.cnblogs.com/Images/dot.gif)
{//如果当前项已经是最末页
hln.Enabled = false;
hlla.Enabled = false;
hlp.Enabled = true;
}
else
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](https://www.cnblogs.com/Images/dot.gif)
{
hln.NavigateUrl = "?page=" + Convert.ToInt32(i+1);
}
![](/Images/OutliningIndicators/InBlock.gif)
DropDownList ddlpage = (DropDownList)e.Item.FindControl("ddlpage");
for (int p = 0; p < n; p++)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](https://www.cnblogs.com/Images/dot.gif)
{
ddlpage.Items.Add(Convert.ToString(p + 1));
if (p == i)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](https://www.cnblogs.com/Images/dot.gif)
{
ddlpage.SelectedIndex = p;
}
}
![](/Images/OutliningIndicators/InBlock.gif)
}
![](/Images/OutliningIndicators/InBlock.gif)
}
![](/Images/OutliningIndicators/InBlock.gif)
protected void ddl_SelectedChanged(object sender, EventArgs e)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](https://www.cnblogs.com/Images/dot.gif)
{
DropDownList ddl = (DropDownList)sender;
Page.Response.Redirect("?page=" +(Convert.ToInt32(ddl.SelectedItem.Value)-1));
}
}
上面是CS
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>
<%@ Import Namespace="System.Data" %>
![](/Images/OutliningIndicators/None.gif)
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
![](/Images/OutliningIndicators/None.gif)
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>无标题页</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:Repeater ID="myRepeater" runat="server" OnItemDataBound="myRepeater_ItemDataBound">
<HeaderTemplate>
<table width="500px" cellspacing="1" cellpadding="0" border="0">
<tr style="background-color:#DFA894">
<th style="height:30px">
编号
</th>
<th>
姓名
</th>
<th>
密码
</th>
<th>
年龄
</th>
<th>
电话
</th>
<th>
邮箱
</th>
</tr>
<tr><td style="height:8px"></td></tr>
</HeaderTemplate>
<ItemTemplate>
<tr>
<td>
<%# DataBinder.Eval(Container.DataItem,"id") %>
</td>
<td>
<%# DataBinder.Eval(Container.DataItem,"username") %>
</td>
<td>
<%# DataBinder.Eval(Container.DataItem,"password") %>
</td>
<td>
<%# DataBinder.Eval(Container.DataItem,"age") %>
</td>
<td>
<%# DataBinder.Eval(Container.DataItem,"tel") %>
</td>
<td>
<a href="mailto:<%# Eval("email") %>"><%# DataBinder.Eval(Container.DataItem,"email") %></a>
</td>
</tr>
<asp:Repeater ID="myRepeater2" runat="server" DataSource='<%# ((DataRowView)Container.DataItem).Row.GetChildRows("myrela") %>'>
<ItemTemplate>
<tr><td style="height:8px"></td></tr>
<tr><td align="center">
</td>
<td style="font-size:9pt">
<%# DataBinder.Eval(Container.DataItem,"["classname"]") %>
</td>
<td style="font-size:9pt">
<%# DataBinder.Eval(Container.DataItem,"["teachername"]") %>
</td>
<td style="font-size:9pt">
<%# DataBinder.Eval(Container.DataItem,"["time"]") %>
</td>
<td style="font-size:9pt">
<%# DataBinder.Eval(Container.DataItem,"["score"]") %>
</td>
<td style="font-size:9pt">
<%# DataBinder.Eval(Container.DataItem,"["slevel"]") %>
</td></tr>
</ItemTemplate>
</asp:Repeater>
</ItemTemplate>
<AlternatingItemTemplate>
<tr>
<td style="color:#000099;">
<%# Eval("id") %>
</td>
<td style="color:#000099;">
<%# Eval("username") %>
</td>
<td style="color:#000099;">
<%# Eval("password") %>
</td>
<td style="color:#000099;">
<%# Eval("age") %>
</td>
<td style="color:#000099;">
<%# Eval("tel") %>
</td>
<td style="color:#000099;">
<a href="mailto:<%# Eval("email") %>"><%# Eval("email") %></a>
</td>
</tr>
<asp:Repeater ID="myRepeater2" runat="server" DataSource='<%# ((DataRowView)Container.DataItem).Row.GetChildRows("myrela") %>'>
<ItemTemplate>
<tr><td style="height:8px"></td></tr>
<tr><td align="center">
</td>
<td style="color:#000099; font-size:9pt">
<%# DataBinder.Eval(Container.DataItem,"["classname"]") %>
</td>
<td style="color:#000099; font-size:9pt">
<%# DataBinder.Eval(Container.DataItem,"["teachername"]") %>
</td>
<td style="color:#000099; font-size:9pt">
<%# DataBinder.Eval(Container.DataItem,"["time"]") %>
</td>
<td style="color:#000099; font-size:9pt">
<%# DataBinder.Eval(Container.DataItem,"["score"]") %>
</td>
<td style="color:#000099; font-size:9pt">
<%# DataBinder.Eval(Container.DataItem,"["slevel"]") %>
</td></tr>
</ItemTemplate>
</asp:Repeater>
</AlternatingItemTemplate>
<SeparatorTemplate>
<tr>
<td colspan="6" style="height:15px;">
<hr style="size:1pt;" />
</td></tr>
</SeparatorTemplate>
<FooterTemplate>
<tr><td style="height:8px"></td></tr>
<tr>
<td colspan="6" style="font-size: 9pt; 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>
跳转到<asp:DropDownList ID="ddlpage" runat="server" AutoPostBack="true" OnSelectedIndexChanged="ddl_SelectedChanged">
</asp:DropDownList>
</td>
</tr>
</table>
</table>
</FooterTemplate>
</asp:Repeater>
</div>
</form>
</body>
</html>
![](/Images/OutliningIndicators/None.gif)
上面是aspx页面的代码
posted @
2007-11-23 11:43
Derek
阅读(
203)
评论()
编辑
收藏
举报