电脑故障

位置:IT落伍者 >> 电脑故障 >> 浏览文章

Repeater和DataList分页


发布日期:2022/11/2
 

因为项目需要要用到DataList和Repeater的分页但这两种加载数据的控件本身并不自带分页功能要控制查询语句来进行分页但是语句过于复杂或是条件太多语句很难写但用PagedDataSource进行分页就简单多了代码分配清晰容易理解

下面实例是用PagedDataSource类实现DataList控件的数据分页也可用于Repeater

页面后台代码

using System;

using SystemData;

using SystemConfiguration;

using SystemCollections;

using SystemWeb;

using SystemWebSecurity;

using SystemWebUI;

using SystemWebUIWebControls;

using SystemWebUIWebControlsWebParts;

using SystemWebUIHtmlControls;

using SystemData;

using SystemDataSqlClient;

public partial class Demo : SystemWebUIPage

{

protected SqlConnection conn; //添加数据库的操作对象

protected SqlDataAdapter da;

protected DataSet ds;

protected SqlCommand comm;

protected void Page_Load(object sender EventArgs e)

{

getArticle();

}

private void getArticle() //取得Article数据

{

conn = new SqlConnection(server=;database=ObtainEmployment;user id=sa;password=;);//取连接字符串建立连接

da = new SqlDataAdapter();

daSelectCommand = new SqlCommand(SELECT top * FROM db_Article where checkup= ORDER BY intime DESC conn);

ds = new DataSet();

try

{

connOpen();

daFill(ds Article);

connClose();

}

catch (SqlException e)

{

ResponseWrite(eToString());

}

int cup = ConvertToInt(thislb_CurrentPageText); //当前页数初始化为地

PagedDataSource ps = new PagedDataSource();

psDataSource = dsTables[Article]DefaultView;

psAllowPaging = true;

psPageSize = ; //每页显示的数据的行数

psCurrentPageIndex = cup ;

lb_countText = psDataSourceCountToString(); //获取记录总数

lb_pageText = psPageCountToString(); //获取总页数

if (!IsPostBack)

{

for (int i = ; i < psPageCount + ; i++)

{

thisDropDownListItemsAdd(iToString());

}

LinkUpEnabled = true;

LinkDownEnabled = true;

}

try

{

DropDownListSelectedItemText = cupToString();

DataListDataSource = ps;

DataListDataBind();

}

catch (Exception ex)

{

throw ex;

}

}

protected void LinkDown_Click(object sender EventArgs e) //下一页按钮代码

{

try

{

lb_CurrentPageText = ConvertToString(ConvertToInt(lb_CurrentPageText) + );

DropDownListSelectedValue = lb_CurrentPageText;

getArticle();

}

catch (Exception ex)

{

ResponseWrite(<script language=javascript> + alert(\已经是最后一页\) + </script>);

lb_CurrentPageText = ;

getArticle();

}

}

protected void DropDownList_SelectedIndexChanged(object sender EventArgs e) //跳转到指定页代码

{

int page =ConvertToInt((DropDownListSelectedItemValue));

lb_CurrentPageText = pageToString();

getArticle();

}

protected void LinkUp_Click(object sender EventArgs e) //上一页按钮代码

{

try

{

if (ConvertToInt(lb_CurrentPageText) > )

{

lb_CurrentPageText = ConvertToString(ConvertToInt(lb_CurrentPageText) );

DropDownListSelectedValue = lb_CurrentPageText;

getArticle();

}

else

{

ResponseWrite(<script language=javascript> + alert(\已经是第一页\) + </script>);

}

}

catch (Exception ex)

{

ResponseWrite(<script language=javascript> + alert(\已经是第一页\) + </script>);

}

}

protected void LinkFirst_Click(object sender EventArgs e) //跳到第一页代码

{

if (lb_CurrentPageText != )

lb_CurrentPageText = ;

else

{

ResponseWrite(<script language=javascript> + alert(\已经是第一页\) + </script>);

}

getArticle();

}

protected void LinkLast_Click(object sender EventArgs e) //跳到最后一页代码

{

if (lb_CurrentPageTextToString() !=lb_pageTextToString())

lb_CurrentPageText = lb_pageTextToString();

else

{

ResponseWrite(<script language=javascript> + alert(\已经是最后一页\) + </script>);

}

getArticle();

}

}

页面前台代码

<%@ Page Language=C# CodeFile=Demoaspxcs Inherits=Demo %>

<!DOCTYPE html PUBLIC //WC//DTD XHTML Transitional//EN transitionaldtd>

<html xmlns= >

<head runat=server>

<title>无标题页</title>

</head>

<body>

<form id=form runat=server>

<div>

<asp:DataList ID=DataList runat=server>

<ItemTemplate>

<asp:Label ID=lbNwes runat=server Text=<%#eval_r(title)%>></asp:Label>

&nbsp;

<asp:Label ID=lbTime runat=server Text=<%#eval_r(intime)%>></asp:Label>

</ItemTemplate>

</asp:DataList></div>

<br />

共<asp:Label ID=lb_count runat=server Text=Label></asp:Label>条记录

&nbsp; &nbsp; &nbsp; 共<asp:Label ID=lb_page runat=server Text=Label></asp:Label>页 &nbsp;

&nbsp; 当前第<asp:Label ID=lb_CurrentPage runat=server Text=></asp:Label>页&nbsp; &nbsp;

&nbsp; &nbsp;

<asp:LinkButton ID=LinkFirst runat=server OnClick=LinkFirst_Click>第一页</asp:LinkButton>

&nbsp; &nbsp;

<asp:LinkButton ID=LinkUp runat=server OnClick=LinkUp_Click>上一页</asp:LinkButton>

&nbsp; &nbsp;<asp:LinkButton ID=LinkDown runat=server OnClick=LinkDown_Click>下一页</asp:LinkButton>

&nbsp; &nbsp;<asp:LinkButton ID=LinkLast runat=server OnClick=LinkLast_Click>最后一页</asp:LinkButton>

&nbsp; &nbsp;转到第<asp:DropDownList ID=DropDownList runat=server OnSelectedIndexChanged=DropDownList_SelectedIndexChanged >

</asp:DropDownList>页

</form>

</body>

</html>

注: PagedDataSource 类的部分公共属性

AllowCustomPaging 获取或设置指示是否启用自定义分页的值

AllowPaging 获取或设置指示是否启用分页的值

Count 获取要从数据源使用的项数

CurrentPageIndex 获取或设置当前页的索引

DataSource 获取或设置数据源

DataSourceCount 获取数据源中的项数

FirstIndexInPage 获取页中的第一个索引

IsCustomPagingEnabled 获取一个值该值指示是否启用自定义分页

IsFirstPage 获取一个值该值指示当前页是否是首页

IsLastPage 获取一个值该值指示当前页是否是最后一页

IsPagingEnabled 获取一个值该值指示是否启用分页

IsReadOnly 获取一个值该值指示数据源是否是只读的

IsSynchronized 获取一个值该值指示是否同步对数据源的访问(线程安全)

PageCount 获取显示数据源中的所有项所需要的总页数

PageSize 获取或设置要在单页上显示的项数

VirtualCount 获取或设置在使用自定义分页时数据源中的实际项数

上一篇:利用Session和HashTable制

下一篇:DataAdapter对象