DataList控件使用初步
1、 添加对AspNetPager.dll的引用,配置Web.config文件,以便使用AspNetPager进行分页。
在Web.config文件的system.web节点下新增配置:
<pages controlRenderingCompatibilityVersion="3.5" clientIDMode="AutoID">
<controls>
<add tagPrefix="webdiyer" namespace="Wuqi.Webdiyer" assembly="AspNetPager" />
</controls>
</pages>
2、 前台页面代码
<asp:DataList ID="DataList1" runat="server" RepeatColumns="3" RepeatDirection="Horizontal"
Width="220px">
<ItemTemplate>
<table style="width: 71%; height: 131px;" class="mytable">
<tr>
<td class="style1">
<img alt="" src="<%#Eval("Image") %>" style="height: 28px; width: 39px" />
</td>
</tr>
<tr>
<td>
<%#Eval("Name") %>
</td>
</tr>
<tr>
<td>
<%#Eval("Price","{0:c2}") %>
</td>
</tr>
</table>
</ItemTemplate>
</asp:DataList>
<table style="width: 100%;" class="mytable">
<tr>
<td>
<webdiyer:AspNetPager ID="AspNetPager1" runat="server" Width="100%" UrlPaging="true"
ShowPageIndexBox="Always" PageIndexBoxType="DropDownList" TextBeforePageIndexBox="Go To Page: "
HorizontalAlign=Center PageSize="2" OnPageChanged="AspNetPager1_PageChanged"
EnableTheming="true" CssClass="paginator" CurrentPageButtonClass="cpb">
</webdiyer:AspNetPager>
</td>
</tr>
</table>
3、 后台代码(结合AspNetPager进行分页)
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
BindData();
}
}
private void BindData()
{
string connStr = "server=.\\sqlexpress;database=codematic;integrated security=true";
using (SqlConnection conn = new SqlConnection(connStr))
{
string sql = "select * from P_Product";
SqlDataAdapter da=new SqlDataAdapter(sql,conn);
DataTable dt = new DataTable();
da.Fill(dt);
DataView dv = dt.DefaultView;
AspNetPager1.RecordCount = dv.Count;
PagedDataSource pds = new PagedDataSource();
pds.DataSource = dv;
pds.AllowPaging = true;
pds.PageSize = AspNetPager1.PageSize;
pds.CurrentPageIndex = AspNetPager1.CurrentPageIndex-1;
DataList1.DataSource = pds;
DataList1.DataBind();
}
}
protected void AspNetPager1_PageChanged(object src, EventArgs e)
{
BindData();
}
如果要提高性能,可以采用数据源分页,请参照AspNetPager使用初步。