Repeater中嵌套R(shí)epeater的示例介紹
更新時(shí)間:2014年01月02日 16:22:31 作者:
在某些特殊情況下是需要在Repeater中嵌套使用Repeater的,下面有個(gè)不錯(cuò)的示例,感興趣的朋友可以參考下
復(fù)制代碼 代碼如下:
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
this.dtCategory = GetCategoryTable();
this.dtProduct = GetProductTable();
rptCategoryList.DataSource = dtCategory;
rptCategoryList.DataBind();
}
}
// 準(zhǔn)備一張分類表
DataTable GetCategoryTable()
{
DataTable dt = new DataTable();
dt.Columns.Add("CategoryId", typeof(int));
dt.Columns.Add("CategoryTitle", typeof(string));
for (int i = 1; i <= 3; i++)
{
DataRow row = dt.NewRow();
row["CategoryId"] = i;
row["CategoryTitle"] = "分類名字 " + i + "";
dt.Rows.Add(row);
}
return dt;
}
// 準(zhǔn)備一張產(chǎn)品表
DataTable GetProductTable()
{
DataTable dt = new DataTable();
dt.Columns.Add("ProductTitle", typeof(string));
dt.Columns.Add("CategoryId", typeof(int));
for (int i = 1; i <= 9; i++)
{
DataRow row = dt.NewRow();
row["ProductTitle"] = "產(chǎn)品名字 " + i + "";
if (i > 6) row["CategoryId"] = 3;
else if (i > 3) row["CategoryId"] = 2;
else row["CategoryId"] = 1;
dt.Rows.Add(row);
}
return dt;
}
// 獲取某個(gè)類別的產(chǎn)品
DataTable GetProductTable(int categoryId)
{
DataView dv = this.dtProduct.DefaultView;
dv.RowFilter = " CategoryId=" + categoryId + " ";
return dv.ToTable();
}
protected void rptCategoryList_ItemDataBound(object sender, RepeaterItemEventArgs e)
{
if (e.Item.ItemType == ListItemType.AlternatingItem || e.Item.ItemType == ListItemType.Item)
{
DataRowView drv = (DataRowView)e.Item.DataItem;
Literal ltlTitle = (Literal)e.Item.FindControl("ltlTitle");
ltlTitle.Text = drv["CategoryTitle"].ToString();
Repeater rptProductList = (Repeater)e.Item.FindControl("rptProductList");
rptProductList.DataSource = GetProductTable(Convert.ToInt32(drv["CategoryId"]));
rptProductList.DataBind();
}
}
protected void rptProductList_ItemDataBound(object sender, RepeaterItemEventArgs e)
{
if (e.Item.ItemType == ListItemType.AlternatingItem || e.Item.ItemType == ListItemType.Item)
{
DataRowView drv = (DataRowView)e.Item.DataItem;
Literal ltlTitle = (Literal)e.Item.FindControl("ltlTitle");
ltlTitle.Text = drv["ProductTitle"].ToString();
}
}
前臺(tái)aspx代碼
復(fù)制代碼 代碼如下:
<</CODE>form id="form1" runat="server">
<</CODE>div>
<</CODE>asp:Repeater ID="rptCategoryList" runat="server" OnItemDataBound="rptCategoryList_ItemDataBound">
<</CODE>ItemTemplate>
<</CODE>div class="listBox">
<</CODE>div class="title">
<</CODE>asp:Literal ID="ltlTitle" runat="server"></</CODE>asp:Literal></</CODE>div>
<</CODE>div class="content">
<</CODE>ul>
<</CODE>asp:Repeater ID="rptProductList" runat="server" OnItemDataBound="rptProductList_ItemDataBound">
<</CODE>ItemTemplate>
<</CODE>li>
<</CODE>asp:Literal ID="ltlTitle" runat="server"></</CODE>asp:Literal>
</</CODE>li>
</</CODE>ItemTemplate>
</</CODE>asp:Repeater>
</</CODE>ul>
</</CODE>div>
</</CODE>div>
</</CODE>ItemTemplate>
</</CODE>asp:Repeat</</CODE>div>
</</CODE>form>
您可能感興趣的文章:
- Repeater事件OnItemCommand取得行內(nèi)控件的方法
- Repeater控件與PagedDataSource結(jié)合實(shí)現(xiàn)分頁(yè)功能
- Repeater控件實(shí)現(xiàn)編輯、更新、刪除等操作示例代碼
- Repeater怎么實(shí)現(xiàn)多行間隔顯示分隔符
- repeater做刪除前彈窗詢問(wèn)實(shí)例
- 給Repeater控件里添加序號(hào)的5種才常見方法介紹
- asp.net Repeater 數(shù)據(jù)綁定的具體實(shí)現(xiàn)(圖文詳解)
- Repeater控件綁定的三種方式
- ASP.NET筆記之 Repeater的使用
- asp.net Repeater分頁(yè)實(shí)例(PageDataSource的使用)
- Repeater里switch的使用方法
- Repeater中添加按鈕實(shí)現(xiàn)點(diǎn)擊按鈕獲取某一行數(shù)據(jù)的方法
相關(guān)文章
ASP.NET UserControl 通信的具體實(shí)現(xiàn)
下面我就用ASP.NET的UserControl模擬SharePoint UserControl通信,兩者的本質(zhì),思想和實(shí)現(xiàn)方式都不變。2013-06-06使用本機(jī)IIS?Express開發(fā)Asp.Net?Core應(yīng)用圖文教程
IIS Express是一個(gè)Mini版的IIS,能夠支持所有的Web開發(fā)任務(wù),本篇經(jīng)驗(yàn)將和大家介紹使用自定義主機(jī)名來(lái)訪問(wèn)運(yùn)行在IIS?Express上的站點(diǎn)程序的方法,希望對(duì)大家的工作和學(xué)習(xí)有所幫助2023-06-06Asp.Net使用Bulk實(shí)現(xiàn)批量插入數(shù)據(jù)
這篇文章主要介紹了Asp.Net使用Bulk實(shí)現(xiàn)批量插入數(shù)據(jù)的方法,對(duì)于進(jìn)行asp.net數(shù)據(jù)庫(kù)程序設(shè)計(jì)非常有借鑒價(jià)值,需要的朋友可以參考下2014-09-09ASP.NET?Core在Task中使用IServiceProvider的問(wèn)題解析
這篇文章主要介紹了解決ASP.NET?Core在Task中使用IServiceProvider的問(wèn)題,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2022-08-08ASP.NET網(wǎng)頁(yè)打印(只打印相關(guān)內(nèi)容/自寫功能)
朋友要求在前段時(shí)間完成的新聞的網(wǎng)站上加上一個(gè)功能,就是在每篇新聞瀏覽的頁(yè)面, 加一個(gè)打印銨鈕。讓用戶一點(diǎn)打印,能把整篇文章打印2013-01-01在asp.net下實(shí)現(xiàn)Option條目中填充前導(dǎo)空格的方法
在asp.net下實(shí)現(xiàn)Option條目中填充前導(dǎo)空格的方法...2007-03-03.net core利用orm如何操作mysql數(shù)據(jù)庫(kù)詳解
這篇文章主要給大家介紹了關(guān)于.net core利用orm如何操作mysql數(shù)據(jù)庫(kù)的相關(guān)資料,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2018-05-05