使用Repeater定制n行m列的数据邦定表
通过定制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>
<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
{
get
{
Object state = ViewState["ListingRows"];
if (state != null)
{
return (int)state;
}
return 3;
}
set
{
ViewState["ListingRows"] = value;
}
}
public int ListingColumns
{
get
{
Object state = ViewState["ListingColumns"];
if (state != null)
{
return (int)state;
}
return 3;
}
set
{
ViewState["ListingColumns"] = value;
}
}
private int columnCount = 0;
private int rowCount = 0;
private int totalColumns = 0;
private bool totalColumnsFound = false;
private int totalRecords = 0;
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
this.BindRoleList();
}
}
protected void roleList_ItemDataBound(object sender, RepeaterItemEventArgs e)
{
PRole pRole = e.Item.DataItem as PRole;
if (pRole != null)
{
switch (e.Item.ItemType)
{
case ListItemType.Item:
case ListItemType.AlternatingItem:
//角色名
Literal roleName = (Literal)e.Item.FindControl("roleName");
roleName.Text = pRole.RoleName;
//设置列数
HtmlTableCell tableColumn = e.Item.FindControl("tableColumn") as HtmlTableCell;
if (tableColumn != null)
tableColumn.Width = (100 / ListingColumns).ToString() + "%";
//Begin a column, if necessary
if (columnCount == 0)
{
Literal beginRow = (Literal)e.Item.FindControl("beginRow");
beginRow.Text = "<tr>";
}
columnCount++;
if (!totalColumnsFound)
totalColumns++;
//End a column, if necessary
if ((columnCount == ListingColumns) || ((rowCount * ListingColumns + columnCount) == totalRecords))
{
Literal endRow = (Literal)e.Item.FindControl("endRow");
endRow.Text = "</tr>";
columnCount = 0;
totalColumnsFound = true;
rowCount++;
}
break;
}
}
}
private void BindRoleList()
{
List<PRole> pRoles = GetDataSource();
totalRecords = pRoles.Count;
roleList.DataSource = pRoles;
roleList.DataBind();
}
private List<PRole> GetDataSource()
{
Guid applictionId = new Guid("7cdbbdea-7b31-42cc-b920-f7a034062063");
IPRole iPRole = ObjectFactory.CreateIPRole();
List<PRole> pRoles = iPRole.GetPRoles(applictionId);
return pRoles;
}
{
get
{
Object state = ViewState["ListingRows"];
if (state != null)
{
return (int)state;
}
return 3;
}
set
{
ViewState["ListingRows"] = value;
}
}
public int ListingColumns
{
get
{
Object state = ViewState["ListingColumns"];
if (state != null)
{
return (int)state;
}
return 3;
}
set
{
ViewState["ListingColumns"] = value;
}
}
private int columnCount = 0;
private int rowCount = 0;
private int totalColumns = 0;
private bool totalColumnsFound = false;
private int totalRecords = 0;
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
this.BindRoleList();
}
}
protected void roleList_ItemDataBound(object sender, RepeaterItemEventArgs e)
{
PRole pRole = e.Item.DataItem as PRole;
if (pRole != null)
{
switch (e.Item.ItemType)
{
case ListItemType.Item:
case ListItemType.AlternatingItem:
//角色名
Literal roleName = (Literal)e.Item.FindControl("roleName");
roleName.Text = pRole.RoleName;
//设置列数
HtmlTableCell tableColumn = e.Item.FindControl("tableColumn") as HtmlTableCell;
if (tableColumn != null)
tableColumn.Width = (100 / ListingColumns).ToString() + "%";
//Begin a column, if necessary
if (columnCount == 0)
{
Literal beginRow = (Literal)e.Item.FindControl("beginRow");
beginRow.Text = "<tr>";
}
columnCount++;
if (!totalColumnsFound)
totalColumns++;
//End a column, if necessary
if ((columnCount == ListingColumns) || ((rowCount * ListingColumns + columnCount) == totalRecords))
{
Literal endRow = (Literal)e.Item.FindControl("endRow");
endRow.Text = "</tr>";
columnCount = 0;
totalColumnsFound = true;
rowCount++;
}
break;
}
}
}
private void BindRoleList()
{
List<PRole> pRoles = GetDataSource();
totalRecords = pRoles.Count;
roleList.DataSource = pRoles;
roleList.DataBind();
}
private List<PRole> GetDataSource()
{
Guid applictionId = new Guid("7cdbbdea-7b31-42cc-b920-f7a034062063");
IPRole iPRole = ObjectFactory.CreateIPRole();
List<PRole> pRoles = iPRole.GetPRoles(applictionId);
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>
<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
{
get
{
Object state = ViewState["ListingRows"];
if (state != null)
{
return (int)state;
}
return 3;
}
set
{
ViewState["ListingRows"] = value;
}
}
public int ListingColumns
{
get
{
Object state = ViewState["ListingColumns"];
if (state != null)
{
return (int)state;
}
return 2;
}
set
{
ViewState["ListingColumns"] = value;
}
}
private int columnCount = 0;
private int rowCount = 0;
private int totalColumns = 0;
private bool totalColumnsFound = false;
private int totalItems = 0;
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
this.BindRoleList(pager.StartRecordIndex);
}
}
protected void roleList_ItemDataBound(object sender, RepeaterItemEventArgs e)
{
PRole pRole = e.Item.DataItem as PRole;
if (pRole != null)
{
switch (e.Item.ItemType)
{
case ListItemType.Item:
case ListItemType.AlternatingItem:
//角色名
Literal roleName = (Literal)e.Item.FindControl("roleName");
roleName.Text = pRole.RoleName;
//设置列数
HtmlTableCell tableColumn = e.Item.FindControl("tableColumn") as HtmlTableCell;
if (tableColumn != null)
tableColumn.Width = (100 / ListingColumns).ToString() + "%";
//Begin a column, if necessary
if (columnCount == 0)
{
Literal beginRow = (Literal)e.Item.FindControl("beginRow");
beginRow.Text = "<tr>";
}
columnCount++;
if (!totalColumnsFound)
totalColumns++;
//End a column, if necessary
if ((columnCount == ListingColumns) || ((rowCount * ListingColumns + columnCount) == totalItems))
{
Literal endRow = (Literal)e.Item.FindControl("endRow");
endRow.Text = "</tr>";
columnCount = 0;
totalColumnsFound = true;
rowCount++;
}
break;
}
}
}
protected void pager_PageChanging(object src, Wuqi.Webdiyer.PageChangingEventArgs e)
{
this.BindRoleList(e.NewPageIndex);
}
private void BindRoleList(int currentIndex)
{
//配置分页控件的页大小
pager.PageSize = ListingColumns*ListingRows;
Guid applictionId = new Guid("7cdbbdea-7b31-42cc-b920-f7a034062063");
int totalRecords = 0;
IPRole iPRole = ObjectFactory.CreateIPRole();
List<PRole> pRoles = iPRole.GetPRoles(applictionId, currentIndex - 1, pager.PageSize, out totalRecords);
totalItems = totalRecords;
//配置分页控件的记录总数
pager.RecordCount = totalRecords;
roleList.DataSource = pRoles;
roleList.DataBind();
}
{
get
{
Object state = ViewState["ListingRows"];
if (state != null)
{
return (int)state;
}
return 3;
}
set
{
ViewState["ListingRows"] = value;
}
}
public int ListingColumns
{
get
{
Object state = ViewState["ListingColumns"];
if (state != null)
{
return (int)state;
}
return 2;
}
set
{
ViewState["ListingColumns"] = value;
}
}
private int columnCount = 0;
private int rowCount = 0;
private int totalColumns = 0;
private bool totalColumnsFound = false;
private int totalItems = 0;
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
this.BindRoleList(pager.StartRecordIndex);
}
}
protected void roleList_ItemDataBound(object sender, RepeaterItemEventArgs e)
{
PRole pRole = e.Item.DataItem as PRole;
if (pRole != null)
{
switch (e.Item.ItemType)
{
case ListItemType.Item:
case ListItemType.AlternatingItem:
//角色名
Literal roleName = (Literal)e.Item.FindControl("roleName");
roleName.Text = pRole.RoleName;
//设置列数
HtmlTableCell tableColumn = e.Item.FindControl("tableColumn") as HtmlTableCell;
if (tableColumn != null)
tableColumn.Width = (100 / ListingColumns).ToString() + "%";
//Begin a column, if necessary
if (columnCount == 0)
{
Literal beginRow = (Literal)e.Item.FindControl("beginRow");
beginRow.Text = "<tr>";
}
columnCount++;
if (!totalColumnsFound)
totalColumns++;
//End a column, if necessary
if ((columnCount == ListingColumns) || ((rowCount * ListingColumns + columnCount) == totalItems))
{
Literal endRow = (Literal)e.Item.FindControl("endRow");
endRow.Text = "</tr>";
columnCount = 0;
totalColumnsFound = true;
rowCount++;
}
break;
}
}
}
protected void pager_PageChanging(object src, Wuqi.Webdiyer.PageChangingEventArgs e)
{
this.BindRoleList(e.NewPageIndex);
}
private void BindRoleList(int currentIndex)
{
//配置分页控件的页大小
pager.PageSize = ListingColumns*ListingRows;
Guid applictionId = new Guid("7cdbbdea-7b31-42cc-b920-f7a034062063");
int totalRecords = 0;
IPRole iPRole = ObjectFactory.CreateIPRole();
List<PRole> pRoles = iPRole.GetPRoles(applictionId, currentIndex - 1, pager.PageSize, out totalRecords);
totalItems = totalRecords;
//配置分页控件的记录总数
pager.RecordCount = totalRecords;
roleList.DataSource = pRoles;
roleList.DataBind();
}