效果图:
aspx文件中的布局:
<asp:Button ID="Button1" runat="server" Text="添加商品" OnClick="Button1_Click" />
<table border="1" style="border-collapse:collapse;width:100%;" >
<asp:Repeater ID="Repeater1" runat="server" >
<%--标题--%>
<HeaderTemplate>
<tr>
<td colspan="6"><b>商品列表</b></td>
</tr>
<tr style="background:#efefef">
<td style="width:80px;"><b>ID</b></td>
<td style="width:80px;"><b>编号</b></td>
<td style="width:80px;"><b>名称</b></td>
<td style="width:80px;"><b>条码</b></td>
<td style="width:80px;"><b>价格(元)</b></td>
<td style="width:auto;"><b>说明</b></td>
</tr>
</HeaderTemplate>
<%--初始化每个repeater item的样式--%>
<ItemTemplate>
<tr>
<td><%# Eval("ID")%></td>
<td><%# Eval("Name")%></td>
<td><%# Eval("SerialNumber")%></td>
<td><%# Eval("Price")%></td>
<td><%# Eval("Specification")%></td>
<td><%# Eval("Directions")%></td>
</tr>
</ItemTemplate>
<%--每隔一组ItemTemplate后 模板的样式--%>
<AlternatingItemTemplate>
<tr style="background:#efefef" >
<td><%# Eval("ID")%></td>
<td><%# Eval("Name")%></td>
<td><%# Eval("SerialNumber")%></td>
<td><%# Eval("Price")%></td>
<td><%# Eval("Specification")%></td>
<td><%# Eval("Directions")%></td>
</tr>
</AlternatingItemTemplate>
</asp:Repeater>
<tr style="height:auto">
<td class=".end-style1"><asp:HyperLink runat="server" ID="first" Text="首页" ></asp:HyperLink></td>
<td class=".end-style1"><asp:HyperLink runat="server" ID="end" Text="末页" ></asp:HyperLink></td>
<td class="end-style1"><asp:HyperLink runat="server" ID="last" Text="上一页" ></asp:HyperLink></td>
<td class="end-style1"><asp:HyperLink runat="server" ID="next" Text="下一页" ValidateRequestMode="Enabled"></asp:HyperLink></td>
<td style="width:80px;">
<asp:TextBox runat="server" ID="skip" Width="30px"></asp:TextBox><asp:LinkButton ID="LinkButton1" runat="server" Text="跳转" OnClick="Unnamed1_Click" Width="40px"></asp:LinkButton>
</td>
<td><asp:Label runat="server" ID="页码标签">当前第N/A页,共N/A页</asp:Label></td>
</tr>
</table>
cs代码:
sqlHelper helper = new sqlHelper();
//通过这个类来实现分页
PagedDataSource pds;
protected void Page_Load(object sender, EventArgs e)
{
DataTable dt = helper.getSqlData("select * from T_Product");
pds = new PagedDataSource();
pds.DataSource = dt.DefaultView;
//允许分页
pds.AllowPaging = true;
//每页有2个数据
pds.PageSize = 2;
int CurPage;
//通过问号传值传进来的page来进行设置应显示的页面
if (Request.QueryString["Page"] != null)
{
CurPage = Convert.ToInt32(Request.QueryString["Page"]);
}
else
{
CurPage = 1;
}
页码标签.Text = "当前第" + CurPage + "页,共" + pds.PageCount+"页";
pds.CurrentPageIndex = CurPage-1;
first.NavigateUrl = Request.CurrentExecutionFilePath + "?Page=1";
end.NavigateUrl = Request.CurrentExecutionFilePath + "?Page=" + pds.PageCount;
last.NavigateUrl = Request.CurrentExecutionFilePath + "?Page=" + (CurPage == 1 ? "1" : CurPage - 1+"");
next.NavigateUrl = Request.CurrentExecutionFilePath + "?Page=" + (CurPage == pds.PageCount ? pds.PageCount + "" : CurPage + 1 + "");
Repeater1.DataSource = pds;
Repeater1.DataBind();
}
//跳转linkbutton的点击事件
protected void Unnamed1_Click(object sender, EventArgs e)
{
try
{
string skipText = skip.Text.ToString().Trim();
int intskipText = int.Parse(skipText);
int page = 0;
if (intskipText > pds.PageCount)
{
page = pds.PageCount;
}
else if (intskipText < 1)
{
page = 1;
}
else
{
page = intskipText;
}
Context.Response.Redirect(Request.CurrentExecutionFilePath + "?Page=" + page);
}
catch (Exception ex)
{
Context.Response.Write("<script>alert('" + ex.Message.ToString() + "');</script>");
}
}