内容:
数据分页
表格效果
一、repeater绑定数据
public class DB
{
public DB()
{
//
// TODO: 在此处添加构造函数逻辑
//
}
public static SqlConnection createCon()
{
return new SqlConnection("server=localhost\\sqlexpress;database=northwind;uid=sa;password=hello");
}
public partial class repeater : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
SqlConnection con = DB.createCon();
SqlDataAdapter sda = new SqlDataAdapter();
sda.SelectCommand = new SqlCommand("select * from employees", con);
DataSet ds = new DataSet();
sda.Fill(ds, "emp");
this.Repeater1.DataSource = ds.Tables["emp"];
this.Repeater1.DataBind();
}
}
}
<head runat="server">
<title>无标题页</title>
</head>
<body>
<form id="form1" runat="server">
<div>
</div>
<asp:Repeater ID="Repeater1" runat="server">
<HeaderTemplate>
<h3>模版页眉></h3>
</HeaderTemplate>
<ItemTemplate>
<%# DataBinder.Eval(Container.DataItem,"LastName") %>
<%# DataBinder.Eval(Container.DataItem,"FirstName") %>
<br />
</ItemTemplate>
<AlternatingItemTemplate>
<font color=blue>
<%# DataBinder.Eval(Container.DataItem,"LastName") %>
<%# DataBinder.Eval(Container.DataItem,"FirstName")%>
<br />
</font>
</AlternatingItemTemplate>
<SeparatorTemplate >
<hr color=blue size=1 width=100 align=left />
</SeparatorTemplate>
<FooterTemplate>
<h3>模版页脚></h3>
</FooterTemplate>
</asp:Repeater>
</form>
</body>
</html>
//运行效果
二、实现数据分页
<head runat="server">
<title>无标题页</title>
</head>
<body>
<form id="form1" runat="server">
<div>
</div>
<asp:Label ID="Label1" runat="server" Style="z-index: 100; left: 15px; position: absolute;
top: 299px" Text="当前页:" Width="64px"></asp:Label>
<asp:Label ID="Label2" runat="server" Style="z-index: 101; left: 93px; position: absolute;
top: 299px" Text="Label" Width="52px"></asp:Label>
<asp:Repeater ID="Repeater1" runat="server">
<HeaderTemplate>
<h3>模版页眉</h3>
</HeaderTemplate>
<ItemTemplate>
<%# DataBinder.Eval(Container.DataItem,"LastName") %>
<%# DataBinder.Eval(Container.DataItem,"FirstName") %>
<br />
</ItemTemplate>
<AlternatingItemTemplate>
<font color=blue>
<%# DataBinder.Eval(Container.DataItem,"LastName") %>
<%# DataBinder.Eval(Container.DataItem,"FirstName")%>
<br />
</font>
</AlternatingItemTemplate>
<SeparatorTemplate >
<hr color=blue size=1 width=100 align=left />
</SeparatorTemplate>
<FooterTemplate>
<h3>模版页脚</h3>
</FooterTemplate>
</asp:Repeater>
<asp:Button ID="Button1" runat="server" OnClick="Button1_Click" Style="z-index: 102;
left: 161px; position: absolute; top: 296px" Text="上一页" />
<asp:Button ID="Button2" runat="server" OnClick="Button2_Click" Style="z-index: 104;
left: 233px; position: absolute; top: 296px" Text="下一页" />
</form>
</body>
</html>
public partial class repeater : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
this.Label2.Text = "1"; //初始页面时,设置当前页码为第一页并显示数据
this.dataBindToRepeater(); //调用dataBindToRepeater()方法
}
}
//使用dataBindToRepeater()方法绑定数据
private void dataBindToRepeater()
{
int curPage = Convert.ToInt32(this.Label2.Text);
SqlConnection con = DB.createCon();
SqlDataAdapter sda = new SqlDataAdapter();
sda.SelectCommand = new SqlCommand("select * from employees", con);
DataSet ds = new DataSet();
sda.Fill(ds, "emp");
System.Web.UI.WebControls.PagedDataSource ps = new PagedDataSource();//实例化PagedDataSource()
ps.DataSource = ds.Tables["emp"].DefaultView; //使用试图绑定数据源
ps.AllowPaging = true;//允许分页
ps.PageSize = 3; //设置分页数
ps.CurrentPageIndex = curPage - 1;
this.Button1.Enabled = true;
this.Button2.Enabled = true;
if (curPage == 1) //如果当前页码为第一页则“上一页”按钮不可用,为灰色
{
Button1.Enabled = false;
}
if (curPage == ps.PageCount)//如果当前页码为最后页则“下一页”按钮不可用,为灰色
{
Button2.Enabled = false;
}
this.Repeater1.DataSource = ps; //定义数据源
this.Repeater1.DataBind(); //绑定数据
}
protected void Button1_Click(object sender, EventArgs e)
{
//单击“上一页”按钮则当前页数减1
this.Label2.Text = Convert.ToString(Convert.ToInt32(this.Label2.Text) - 1);
this.dataBindToRepeater();
}
{
//单击“下一页”按钮则当前页数加1
this.Label2.Text=Convert.ToString(Convert.ToInt32(this.Label2.Text)+1);
this.dataBindToRepeater();
}
}
//分页运行效果
三、将Repeater数据设置表格格式
<head runat="server">
<title>无标题页</title>
</head>
<body>
<form id="form1" runat="server">
<div>
</div>
<asp:Label ID="Label1" runat="server" Style="z-index: 100; left: 15px; position: absolute;
top: 299px" Text="当前页:" Width="64px"></asp:Label>
<asp:Label ID="Label2" runat="server" Style="z-index: 101; left: 93px; position: absolute;
top: 299px" Text="Label" Width="52px"></asp:Label>
<asp:Repeater ID="Repeater1" runat="server">
<HeaderTemplate>
<h3>模版页眉</h3>
<table border=1 cellspacing=0 bordercolor=blue>
<tr>
<td align=center>员工姓名
</td>
</tr>
</HeaderTemplate>
<ItemTemplate>
<tr>
<td><%# DataBinder.Eval(Container.DataItem,"LastName") %>
<%# DataBinder.Eval(Container.DataItem,"FirstName") %>
</td>
</tr>
</ItemTemplate>
<AlternatingItemTemplate>
<tr>
<td><%# DataBinder.Eval(Container.DataItem,"LastName") %>
<%# DataBinder.Eval(Container.DataItem,"FirstName")%>
</td>
</tr>
</AlternatingItemTemplate>
<SeparatorTemplate >
</SeparatorTemplate>
<FooterTemplate>
</table>
<h3>模版页脚</h3>
</FooterTemplate>
</asp:Repeater>
<asp:Button ID="Button1" runat="server" OnClick="Button1_Click" Style="z-index: 102;
left: 161px; position: absolute; top: 296px" Text="上一页" />
<asp:Button ID="Button2" runat="server" OnClick="Button2_Click" Style="z-index: 104;
left: 233px; position: absolute; top: 296px" Text="下一页" />
</form>
</body>
</html>
//表格效果(可以设置更丰富的效果)