通过定制n行m列的数据表而不是常用的n行1列数据表可以有效利用页面空间,下面是一个例子:
<asp:Repeater ID="roleList" runat="server" OnItemDataBound="roleList_ItemDataBound">
<HeaderTemplate>
<table class="Listing_table" border="0" cellpadding="0" cellspacing="0" align="center" width="100%">
</HeaderTemplate>
<ItemTemplate>
<asp:Literal id="beginRow" runat="server" />
<td valign="top" id="tableColumn" runat="server">
<div class="listContainer">
<asp:Literal ID="roleName" runat="server" />
</div>
</td>
<asp:Literal id="endRow" runat="server" />
</ItemTemplate>
<FooterTemplate>
</table>
</FooterTemplate>
</asp:Repeater>
public int ListingRows
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
{
get
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
Object state = ViewState["ListingRows"];
if (state != null)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
return (int)state;
}
return 3;
}
set
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
ViewState["ListingRows"] = value;
}
}
public int ListingColumns
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
{
get
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
Object state = ViewState["ListingColumns"];
if (state != null)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
return (int)state;
}
return 3;
}
set
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
ViewState["ListingColumns"] = value;
}
}
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
private int columnCount = 0;
private int rowCount = 0;
private int totalColumns = 0;
private bool totalColumnsFound = false;
private int totalRecords = 0;
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
protected void Page_Load(object sender, EventArgs e)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
{
if (!IsPostBack)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
this.BindRoleList();
}
}
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
protected void roleList_ItemDataBound(object sender, RepeaterItemEventArgs e)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
{
PRole pRole = e.Item.DataItem as PRole;
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
if (pRole != null)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
switch (e.Item.ItemType)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
case ListItemType.Item:
case ListItemType.AlternatingItem:
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
//角色名
Literal roleName = (Literal)e.Item.FindControl("roleName");
roleName.Text = pRole.RoleName;
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
//设置列数
HtmlTableCell tableColumn = e.Item.FindControl("tableColumn") as HtmlTableCell;
if (tableColumn != null)
tableColumn.Width = (100 / ListingColumns).ToString() + "%";
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
//Begin a column, if necessary
if (columnCount == 0)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
Literal beginRow = (Literal)e.Item.FindControl("beginRow");
beginRow.Text = "<tr>";
}
columnCount++;
if (!totalColumnsFound)
totalColumns++;
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
//End a column, if necessary
if ((columnCount == ListingColumns) || ((rowCount * ListingColumns + columnCount) == totalRecords))
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
Literal endRow = (Literal)e.Item.FindControl("endRow");
endRow.Text = "</tr>";
columnCount = 0;
totalColumnsFound = true;
rowCount++;
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
break;
}
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
}
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
private void BindRoleList()
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
{
List<PRole> pRoles = GetDataSource();
totalRecords = pRoles.Count;
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
roleList.DataSource = pRoles;
roleList.DataBind();
}
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
private List<PRole> GetDataSource()
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
{
Guid applictionId = new Guid("7cdbbdea-7b31-42cc-b920-f7a034062063");
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
IPRole iPRole = ObjectFactory.CreateIPRole();
List<PRole> pRoles = iPRole.GetPRoles(applictionId);
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
return pRoles;
}
分页状态下的使用:
<div>
<asp:Repeater ID="roleList" runat="server" OnItemDataBound="roleList_ItemDataBound">
<HeaderTemplate>
<table class="Listing_table" border="0" cellpadding="0" cellspacing="0" align="center" width="100%">
</HeaderTemplate>
<ItemTemplate>
<asp:Literal id="beginRow" runat="server" />
<td valign="top" id="tableColumn" runat="server">
<div class="listContainer">
<asp:Literal ID="roleName" runat="server" />
</div>
</td>
<asp:Literal id="endRow" runat="server" />
</ItemTemplate>
<FooterTemplate>
</table>
</FooterTemplate>
</asp:Repeater>
</div>
<div>
<webdiyer:AspNetPager ID="pager" runat="server" Width="100%" OnPageChanging="pager_PageChanging"
UrlPaging="true"
HorizontalAlign="right"
PageSize="5"
EnableTheming="true"
NumericButtonCount="3"
UrlPageIndexName="p"
NumericButtonTextFormatString="[{0}]"
ShowInputBox="Always"
SubmitButtonText="go"
TextBeforeInputBox="跳到"
TextAfterInputBox="页"
ShowCustomInfoSection="Left"
CustomInfoHTML="页次:<font color='red'>%currentPageIndex%</font>/%pageCount%页 <font color='red'>%PageSize%</font>个内容/页 共有<font color='red'>%RecordCount%</font>个内容"
FirstPageText="<<"
LastPageText=">>"
PrevPageText="<"
NextPageText=">"
Font-Size="Smaller"
ShowNavigationToolTip="true"
>
</webdiyer:AspNetPager>
</div>
public int ListingRows
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
{
get
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
Object state = ViewState["ListingRows"];
if (state != null)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
return (int)state;
}
return 3;
}
set
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
ViewState["ListingRows"] = value;
}
}
public int ListingColumns
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
{
get
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
Object state = ViewState["ListingColumns"];
if (state != null)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
return (int)state;
}
return 2;
}
set
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
ViewState["ListingColumns"] = value;
}
}
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
private int columnCount = 0;
private int rowCount = 0;
private int totalColumns = 0;
private bool totalColumnsFound = false;
private int totalItems = 0;
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
protected void Page_Load(object sender, EventArgs e)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
{
if (!IsPostBack)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
this.BindRoleList(pager.StartRecordIndex);
}
}
protected void roleList_ItemDataBound(object sender, RepeaterItemEventArgs e)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
{
PRole pRole = e.Item.DataItem as PRole;
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
if (pRole != null)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
switch (e.Item.ItemType)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
case ListItemType.Item:
case ListItemType.AlternatingItem:
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
//角色名
Literal roleName = (Literal)e.Item.FindControl("roleName");
roleName.Text = pRole.RoleName;
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
//设置列数
HtmlTableCell tableColumn = e.Item.FindControl("tableColumn") as HtmlTableCell;
if (tableColumn != null)
tableColumn.Width = (100 / ListingColumns).ToString() + "%";
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
//Begin a column, if necessary
if (columnCount == 0)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
Literal beginRow = (Literal)e.Item.FindControl("beginRow");
beginRow.Text = "<tr>";
}
columnCount++;
if (!totalColumnsFound)
totalColumns++;
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
//End a column, if necessary
if ((columnCount == ListingColumns) || ((rowCount * ListingColumns + columnCount) == totalItems))
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
Literal endRow = (Literal)e.Item.FindControl("endRow");
endRow.Text = "</tr>";
columnCount = 0;
totalColumnsFound = true;
rowCount++;
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
break;
}
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
}
protected void pager_PageChanging(object src, Wuqi.Webdiyer.PageChangingEventArgs e)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
{
this.BindRoleList(e.NewPageIndex);
}
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
private void BindRoleList(int currentIndex)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
{
//配置分页控件的页大小
pager.PageSize = ListingColumns*ListingRows;
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
Guid applictionId = new Guid("7cdbbdea-7b31-42cc-b920-f7a034062063");
int totalRecords = 0;
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
IPRole iPRole = ObjectFactory.CreateIPRole();
List<PRole> pRoles = iPRole.GetPRoles(applictionId, currentIndex - 1, pager.PageSize, out totalRecords);
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
totalItems = totalRecords;
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
//配置分页控件的记录总数
pager.RecordCount = totalRecords;
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
roleList.DataSource = pRoles;
roleList.DataBind();
}