用datalist制作一个分页,具体要是实现的情况是:<< << 1 2 3 4 5 6…… >> >> 共多少页 转到多少页,点第一个<<转到首页 点第二个<<向上一页 点数字的时候就转到数字所在的页面,后面同理,做分页, 不管是datalist, 还是grid还是listbox等来实现数据呈现,只要将数据源分页就解决了,构思一个数据源分页的方法,不管点击哪一页,
传个页码的关键字进来这个方法就行了.
******************************
<%@ import namespace="System"%>
<%@ import namespace="System.Data"%>
<%@ import namespace="System.Data.OleDb"%>
<%@ import namespace="System.Data.SqlClient"%>
<%@ import namespace="System.Web.UI"%>
<%@ import namespace="System.Web.UI.WebControls"%>
<%@ import namespace="System.Web.UI.HtmlControls"%>
<%@ import namespace="System.Collections"%>
<%@ import namespace="System.Web.SEssionState"%>
<%@ import namespace="System.Configuration"%>
<script language="c#" runat="server">
OleDbConnection MyConn;
int PageSize,RecordCount,PageCount,CurrentPage,i;
ArrayList Al_PageNum;
public void Page_Load(Object src,EventArgs e)
{
PageSize = 4; //设定PageSize
//连接语句
string strcon="provider=Microsoft.jet.OLEDB.4.0;data Source="+Server.MapPath(ConfigurationSettings.AppSettings["MDBpath"])+";";
MyConn = new OleDbConnection(strcon);
MyConn.Open();
if(!Page.IsPostBack) //第一次请求执行
{
RecordCount = CalculateRecord(); //计算总共有多少记录/
PageCount = RecordCount/PageSize; //计算总共有多少页
if (RecordCount%PageSize > 0) //取整
PageCount = PageCount + 1;
lblPageCount.Text = PageCount.ToString();
lblRecordCount.Text = RecordCount.ToString();
ViewState["PageCount"] = PageCount;
CurrentPage = 0;
ViewState["PageIndex"] = 0;
Al_PageNum= new ArrayList();//绑定DROPDOWNLIST
for(i=1;i<=PageCount;i++) //从1开始循环,为了不出现0页码
Al_PageNum.Add(i.ToString());
Ddl_PageNumber.DataSource=Al_PageNum;
Ddl_PageNumber.DataBind();
ListBind(); //绑定
}
MyConn.Close();
}
protected string FormatString(string str)
{
str=str.Replace(" "," ");
str=str.Replace("<","<");
str=str.Replace(">",">");
str=str.Replace('\n'.ToString(),"<br>");
return str;
}
public int CalculateRecord() //计算总共有多少条记录
{
int intCount;
string strCount = "select count(*) as co from gbook ";
OleDbCommand MyComm = new OleDbCommand(strCount,MyConn);
OleDbDataReader dr = MyComm.ExecuteReader();
if(dr.Read())
{
intCount = Int32.Parse(dr["co"].ToString());
}
else
{
intCount = 0;
}
dr.Close();
return intCount;
}
ICollection CreateSource()
{
int StartIndex; //设定导入的起终地址
StartIndex = CurrentPage*PageSize; //计算记录数的起始点
string strSel = "select * from gbook order by id desc";
DataSet ds = new DataSet();
OleDbDataAdapter MyAdapter = new OleDbDataAdapter(strSel,MyConn);
MyAdapter.Fill(ds,StartIndex,PageSize,"Score");
return ds.Tables["Score"].DefaultView;
}
public void ListBind()
{
MyList.DataSource = CreateSource();
MyList.DataBind();
lbnNextPage.Enabled = true;
lbnPrevPage.Enabled = true;
if(PageCount==0)
{
lblCurrentPage.Text = "0";
lbnNextPage.Enabled = false;
lbnPrevPage.Enabled = false;
}
else
{
if(CurrentPage==(PageCount-1)) lbnNextPage.Enabled = false;
if(CurrentPage==0) lbnPrevPage.Enabled = false;
lblCurrentPage.Text = (CurrentPage+1).ToString();
}
}
public void Page_OnClick(Object sender,CommandEventArgs e)
{
CurrentPage = (int)ViewState["PageIndex"];
PageCount = (int)ViewState["PageCount"];
string cmd = e.CommandName; //判断cmd,以判定翻页方向
switch(cmd)
{
case "next":
if(CurrentPage<(PageCount-1)) CurrentPage++;
break;
case "prev":
if(CurrentPage>0) CurrentPage--;
break;
}
ViewState["PageIndex"] = CurrentPage;
ListBind();
}
public void PageNum_SelectIndexChanged(object sender,System.EventArgs e)
{
ViewState["PageIndex"] =int.Parse(Ddl_PageNumber.SelectedItem.Value)-1;//保持不出现0页码
PageSize = 4;
CurrentPage = (int)ViewState["PageIndex"];
PageCount = (int)ViewState["PageCount"];
ListBind();
//MyList.DataSource = CreateSource();
//MyList.DataBind();
}
******************************
override protected void OnInit(EventArgs e)
{
InitializeComponent();
base.OnInit(e);
}
private void InitializeComponent()
{
this.Load+=new System.EventHandler(this.Page_Load);
this.Ddl_PageNumber.SelectedIndexChanged+=new System.EventHandler(this.PageNum_SelectIndexChanged);
}
public string test(string str)
{
string str2="";
if(str==String.Empty)
{
str2="暂无回复";
}
else
{
string str3="回复:<br> ";
str2=str3+str;
}
return str2;
}
/* for(int i=10;i<20;i++)
{
DropDownList1.Items.Add(new ListItem("第"+i.ToString()+"页",i.ToString()));
}
*/
</script>
<asp:DataList id="MyList" RepeatColumns="1" runat="server">
<itemtemplate> <table width="500" border="0" cellpadding="4" cellspacing="1" bgcolor="8c9aa5">
<tr bgcolor="cecfd6">
<td colspan="2" align="left" bgcolor="#dedbde"><strong> No.<%# DataBinder.Eval(Container.DataItem, "id") %>
<%# DataBinder.Eval(Container.DataItem, "title") %></strong>
</td>
</tr>
<tr>
<td width="95" rowspan="2" align="center" valign="middle" bgcolor="f7f7f7" class="word">
<table width="95" border="0" cellspacing="0" cellpadding="0" class="table1">
<tr>
<td align="center" valign="top" ><br><image src="<%# DataBinder.Eval(Container.DataItem, "sex_image") %>" border=0 ></td>
</tr>
<tr>
<td align="center" class="word" ><%# DataBinder.Eval(Container.DataItem, "username") %><br><%# DataBinder.Eval(Container.DataItem, "sex") %></td>
</tr>
</table>
</td>
<td width="386" height="129" valign="top" bgcolor="f7f7f7" class="word">
<table width="100%" border="0" cellspacing="0" cellpadding="0">
<tr>
<td class="word">留言:<Br> <%# DataBinder.Eval(Container.DataItem, "content") %><br><br><Br><BR></td>
</tr>
<tr>
<td width="80%" bgcolor="#c6c3c6" height="1"></td>
</tr>
<tr>
<td class="word"><br><font color=red><%# test(DataBinder.Eval(Container.DataItem, "r_content").ToString()) %><font></FONT><br><br></td>
</tr>
</table>
</td>
</tr>
<tr>
<td height="22" bgcolor="cecfd6" valign="top">
<table width="100%" border="0" cellspacing="0" cellpadding="0">
<tr>
<td align="left"><table width="100%" border="0" cellspacing="0" cellpadding="0">
<tr>
<td>IP地址:<%# DataBinder.Eval(Container.DataItem, "ip") %>   发表时间:<%# DataBinder.Eval(Container.DataItem, "regdate") %></td>
<td width="15%" align="right"><a href="<%# DataBinder.Eval(Container.DataItem, "website") %>" target="_blank"><img src="img/url.gif" border=0></a> <a href="mailto:<%# DataBinder.Eval(Container.DataItem, "email") %>" target="_blank"><img src="img/email2.gif" border=0></a></td>
</tr>
</table>
</td>
</tr>
</table></td>
</tr>
</table>
<br>
</itemtemplate>
</asp:DataList>
<table width="530" border="1" align="center" cellspacing="0" cellpadding="3" bgcolor="#FFFFFF" bordercolor="#C0C0C0" style="border-collapse: collapse">
<tr>
<td align="right" bgcolor="#ECECEC" colspan="2"><asp:LinkButton id="lbnPrevPage" Text="上一页" CommandName="prev" OnCommand="Page_OnClick" runat="server" />
<asp:LinkButton id="lbnNextPage" Text="下一页" CommandName="next" OnCommand="Page_OnClick" runat="server" />
共有<asp:Label id="lblRecordCount" ForeColor="red" runat="server" />条记录
当前为<asp:Label id="lblCurrentPage" ForeColor="red" runat="server" />/<asp:Label id="lblPageCount" ForeColor="red" runat="server" />页 <asp:dropdownlist ID="Ddl_PageNumber" runat="server" AutoPostBack="true" CssClass="lanyu"></asp:dropdownlist>
</td>
</tr>
</table>