读书人

datalist怎么实现有1234这样的分

发布时间: 2011-12-30 23:30:45 作者: rapoo

datalist如何实现有1,2,3,4这样的分页?

datalist如何实现这样的分页功能? access数据库

首页 上一页 1 2 3 4 5 6 7 9 10 下一页 尾页

或者这样:

首页 1 2 3 4 5 6 7 9 10 尾页



[解决办法]
我也想知道
[解决办法]
http://zhidao.baidu.com/question/4785848.html
[解决办法]
帮顶,学习中~~~
[解决办法]
aspnetpager6.0分页控件
[解决办法]
帮顶!学习学习
[解决办法]
http://www.webdiyer.com/AspNetPager/default.aspx

aspnetpager6.0
[解决办法]
netpager6.0
对于datalist的分页好像需要储存过程~
[解决办法]
可以用分页控件+存储过程
[解决办法]
用list直接去读取grid里面的数据.在grid里面可以实现分页啊...
[解决办法]
顶起来 我也在关注这个问题

我可以实现分页 但是如果删除了效果就不好了

比方说 一页显示6条记录`~

第一页就是1——6条记录 如果我把3 4删了 那么第一页就显示1 2 5 6 4条记录

求解决办法?
有人说建一个虚拟表?还有其他办法吗?
[解决办法]
可以 运行aspnet分页控件。。。
如果想自己写的话可以运用repeater控件动态指定页码,触发repeater的事件从而给dl指定页码获取数据。。
//aspx代码:
<asp:Repeater ID= "repPageIndex " runat= "server " OnItemCommand= "repPageIndex_ItemCommand ">
<ItemTemplate> [ <asp:LinkButton ID= "lnkBtn " runat= "server " CommandName= "Edit " Text= ' <%# Eval( "IndexID ") %> '> </asp:LinkButton> ]  </ItemTemplate>
</asp:Repeater>
//cs代码:大概的:
//给Repeater指定数据源
public void GetPageIndex()
{
DataTable dt = new DataTable();
int repIndex = int.Parse(ViewState[ "RepIndex "].ToString());

dt.Columns.Add( "IndexID ");
int countPage = int.Parse(ViewState[ "CountPage "].ToString());

int indexMaxPage = (repIndex + 1) * 10;//没组的最大索引ID 如:1,2,3,4,5,6,7,8,9,10 则是10,10-20 则是20
int indexMinPage = (repIndex * 10) + 1;

if (countPage < 10)//GV页码不足10
{
lnkNextIndex.Enabled = false;
lnkUpIndex.Enabled = false;
//lnkFirstIndex.Enabled = false;
//lnkLastIndex.Enabled = false;
for (int num = indexMinPage; num <= countPage; num++)
{
DataRow row = dt.NewRow();
row[0] = num;
dt.Rows.Add(row);
lnkNextIndex.Enabled = false;
lnkUpIndex.Enabled = true;
}
}
else
{
if (countPage < indexMaxPage)
{
for (int num = indexMinPage; num <= countPage; num++)
{
DataRow row = dt.NewRow();
row[0] = num;
dt.Rows.Add(row);


lnkNextIndex.Enabled = false;
lnkUpIndex.Enabled = true;
}
}
else
{
for (int num = indexMinPage; num <= indexMaxPage; num++)
{
DataRow row = dt.NewRow();
row[0] = num;
dt.Rows.Add(row);
lnkNextIndex.Enabled = true;
if (indexMinPage <= 1)
lnkUpIndex.Enabled = false;
else
lnkUpIndex.Enabled = true;
}
}
}
if (indexMaxPage == countPage)
{
lnkNextIndex.Enabled = false;
}
repPageIndex.DataSource = dt;
repPageIndex.DataBind();
dt.Clear();
}
[解决办法]
给你2种实现的代码
1)
<% @ Page Language= "C# " %>
<% @ Import Namespace= "System.Data " %>
<% @ Import Namespace= "System.Data.OleDb " %>
<Script Language= "C# " Runat= "Server ">
/*
Create By 飞刀
http://www.aspcn.com
2001-7-25 01:44
Support .Net Framework Beta 2
*/
OleDbConnection MyConn;
int PageSize,RecordCount,PageCount,CurrentPage;
public void Page_Load(Object src,EventArgs e)
{
//设定PageSize
PageSize = 10;

//连接语句
string MyConnString = "Provider=Microsoft.Jet.OLEDB.4.0; Data Source= "+Server.MapPath( ". ")+ "..\\DataBase\\db1.mdb; ";
MyConn = new OleDbConnection(MyConnString);
MyConn.Open();

//第一次请求执行
if(!Page.IsPostBack)
{
ListBind();
CurrentPage = 0;
ViewState[ "PageIndex "] = 0;

//计算总共有多少记录
RecordCount = CalculateRecord();
lblRecordCount.Text = RecordCount.ToString();

//计算总共有多少页
PageCount = RecordCount/PageSize;
lblPageCount.Text = PageCount.ToString();
ViewState[ "PageCount "] = PageCount;
}
}
//计算总共有多少条记录
public int CalculateRecord()
{
int intCount;
string strCount = "select count(*) as co from Score ";
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 Score ";
DataSet ds = new DataSet();

OleDbDataAdapter MyAdapter = new OleDbDataAdapter(strSel,MyConn);
MyAdapter.Fill(ds,StartIndex,PageSize, "Score ");

return ds.Tables[ "Score "].DefaultView;
}
public void ListBind()
{
score.DataSource = CreateSource();
score.DataBind();

lbnNextPage.Enabled = true;
lbnPrevPage.Enabled = true;
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();

}
</script>
<html>
<head>
<title> </title>
</head>
<body>
<form 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:DataList id= "score " runat= "server "
HeaderStyle-BackColor= "#aaaadd "
AlternatingItemStyle-BackColor= "Gainsboro "
EditItemStyle-BackColor= "yellow "
>
<ItemTemplate>
姓名: <%# DataBinder.Eval(Container.DataItem, "Name ") %>
<asp:LinkButton id= "btnSelect " Text= "编辑 " CommandName= "edit " runat= "server " />
</ItemTemplate>
</asp:DataList>
<asp:LinkButton id= "lbnPrevPage " Text= "上一页 " CommandName= "prev " OnCommand= "Page_onClick " runat= "server " />
<asp:LinkButton id= "lbnNextPage " Text= "下一页 " CommandName= "next " OnCommand= "Page_onClick " runat= "server " />

</form>
</body>
</html>

2)
DataList分页2
Repeater和DataList控件提供了一个快速、灵活的表现数据的方式,但是,它们没有内建的分页功能;DataGrid控件提供了内建的分页功能,但它的结构比较复杂。下面就用PagedDataSource类实现Repeater和DataList的分页。 PagedDataSource封装了DataGrid的分页属性,我们可以象DataGrid那样进行分页。代码如下:

<%@ Page Language= "C# " %>
<%@ import namespace= "System.Data " %>
<%@ import namespace= "System.Data.OleDb " %>
<script language= "C# " runat= "server ">
public void Page_Load(Object src,EventArgs e) {
OleDbConnection objConn=new OleDbConnection( "Provider=Microsoft.Jet.OLEDB.4.0; Data Source= " +
Server.MapPath( "../aspxWeb.mdb "));
OleDbDataAdapter objCommand=new OleDbDataAdapter( "select * from Document ",objConn);
DataSet ds=new DataSet();
objCommand.Fill(ds);
PagedDataSource objPds = new PagedDataSource();
objPds.DataSource = ds.Tables[0].DefaultView;
objPds.AllowPaging = true;
objPds.PageSize = 5;
int CurPage;
if (Request.QueryString[ "Page "] != null)
CurPage=Convert.ToInt32(Request.QueryString[ "Page "]);
else
CurPage=1;

objPds.CurrentPageIndex = CurPage-1;
lblCurrentPage.Text = "当前页: " + CurPage.ToString();

if (!objPds.IsFirstPage)
lnkPrev.NavigateUrl=Request.CurrentExecutionFilePath + "?Page= " + Convert.ToString(CurPage-1);

if (!objPds.IsLastPage)
lnkNext.NavigateUrl=Request.CurrentExecutionFilePath+ "?Page= " + Convert.ToString(CurPage+1);

Repeater1.DataSource=objPds;


Repeater1.DataBind();
}
</script>
<html>
<head>
<title> Repeater控件分页的例子 </title>
<meta http-equiv= "Content-Type " content= "text/html; charset=gb2312 ">
<style>
P,TD,DIV,SPAN {font-size:9pt}
</style>
</head>
<body>
<form name= "form1 " method= "POST " runat= "server ">
<div style= "padding:5px;background-color:#dedede ">
<asp:label ID= "lblCurrentPage " runat= "server "> </asp:label> </td>
<td> <asp:HyperLink id= "lnkPrev " runat= "server "> 上一页 </asp:HyperLink>
<asp:HyperLink id= "lnkNext " runat= "server "> 下一页 </asp:HyperLink>
</div>
<hr size= "1 " color= "#000099 "/>
<asp:Repeater ID= "Repeater1 " runat= "server ">
<Itemtemplate>
<div style= "padding:5px;background-color:#dedede ">
<%# DataBinder.Eval(Container.DataItem, "Title ") %>
</div>
</Itemtemplate>
</asp:Repeater>
</form>
</body>
</html>


[解决办法]
读出pagecount,用循环在页面里写1234...如:
string s= " <ul> <li> <a href= 'XXX.aspx?index=0 '> 1 </a> </li> .... </ul> ";
[div].innerHtml = s;
根据记录数设置该显示多少,操作字符和数字应该不难吧,多来几个if{}else{}就行了。
然后把相应index传给CurrentPageIndex,实现跳转。

读书人网 >asp.net

热点推荐