关于datalist分页
一:前台界面:
<asp:DataList ID="DataList1" runat="server" Width="100%" DataKeyField="mediaName" >
<ItemTemplate>
<table width="100%">
<tr>
<td style="width: 20px; height: 22px; vertical-align: middle; text-align: left;">
<asp:Image ID="Image1" runat="server" ImageUrl="~/images/jt02.gif" style="text-align: left" /></td>
<td style="width: 300px; height: 22px; text-align: left; font-size: 12px;">
<%# Eval("mediaName") %>
</td>
<td style="width: auto; height: 22px">
</td>
<td style="width:100px; height: 22px;text-align: right;">
<a href="sourcexiangxi.aspx?medianame=<%# DataBinder.Eval(Container.DataItem,"mediaName")%>" style="font-size: 12px;">查看详情</a>
</td>
</tr>
</table>
</ItemTemplate>
<HeaderTemplate>
<table width="100%" style="background-color: lightgrey">
<tr>
<td style="width: 20px; height: 30px; vertical-align: middle; text-align: left;">
</td>
<td style="width: 300px; height: 30px; text-align: left; font-size: 12px;">
视频名称
</td>
<td style="width: auto; height: 30px">
</td>
<td style="width:100px; height: 30px">
<a href="sourcexiangxi.aspx?medianame=<%# DataBinder.Eval(Container.DataItem,"mediaName")%>" style="font-size: 12px; text-align: right;"></a>
</td>
</tr>
</table>
</HeaderTemplate>
</asp:DataList>
2:然后放一个table在其页面
<table style="background-color:lightgrey; width:100%">
<tr>
<td style=" width:10%; height: 30px;">
<asp:Label ID="labtotle" runat="server" style="font-weight: normal; font-size: 12px; font-family: 宋体"></asp:Label></td>
<td style=" width:14%; height: 30px;">
<asp:Label ID="labcurrpagenum" runat="server" style="font-weight: normal; font-size: 12px; font-family: 宋体"></asp:Label></td>
<td style="height: 30px">
<mp:mypaper id="MyPaper1" runat="server" backcolor="LightGray" borderwidth="0px" cellspacing="0px" firstlink="首页" height="14px" lastlink="尾页" linkistext="True" pagesize="10" style="font-weight: normal; font-size: 12px; font-family: 宋体" CurrentNumberBgColor="DodgerBlue" Width="340px" Font-Bold="False" Font-Size="12px"></mp:mypaper>
</td>
</tr>
</table>
但要在页面开始加上:
<%@ Register Assembly="page" Namespace="MyPaperControls" TagPrefix="MP" %>
二:后台界面
public partial class source : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
fenye();
}
}
private void fenye()
{
feny f = new feny();
MyPaper1.RecordCount = f.countmedia();
string aa = MyPaper1.PageTotal.ToString();
string bb = MyPaper1.CurrentPageIndex.ToString();
labtotle.Text = "共<font color=red>" + aa + "</font>页";
labcurrpagenum.Text = "当前为<font color=red>" + bb + "</font>页";
DataSet ds = new DataSet();
f.selectallmeida().Fill(ds, MyPaper1.DataSet_StartIndex, MyPaper1.PageSize, "table");
this.DataList1.DataSource = ds;
this.DataList1.DataBind();
}
}
三:类
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;
using System.Collections;
/// <summary>
/// feny 的摘要说明
/// </summary>
public class feny
{
private string constring = ConfigurationManager.ConnectionStrings["ycjxdb"].ConnectionString;
public feny()
{
//
// TODO: 在此处添加构造函数逻辑
//
}
public SqlDataAdapter selectallmeida()
{
SqlConnection con = new SqlConnection(constring);
con.Open();
SqlDataAdapter sda = new SqlDataAdapter("selectmedia", con);
sda.SelectCommand.CommandType = CommandType.StoredProcedure;
return sda;
con.Close();
}
//获取总的视频数
public int countmedia()
{
SqlConnection con = new SqlConnection(constring);
con.Open();
SqlCommand cmd = new SqlCommand("countmedia", con);
cmd.CommandType = CommandType.StoredProcedure;
try
{
int media_num = Convert.ToInt32(cmd.ExecuteScalar());
return media_num;
}
catch (Exception err)
{
throw new System.Exception(err.Message);
}
finally
{
con.Close();
}
}