Repeater嵌套问题,可以简单的理解为双重for循环。原理其实是一样的,下面是我在项目中摸索的,编译通过,效果明显。呵呵, 好了
源码如下:
[c-sharp] view plaincopyprint?
- <div style="width: 924px; height: 278px; line-height:20px;">
- <asp:Repeater ID="Repeater1" runat="server" OnItemDataBound="Repeater1_ItemDataBound">
- <ItemTemplate>
- <div class="wenzi" style="background: #F9F7F7; width: 924px; height: 293px; border: 1px dashed #CCCCCC;
- margin: auto; margin-top: 8px; border-top: #FC89A1 solid 1px;">
- <div style="width: 350px; height: 275px; float: left; margin:20px auto auto 10px;">
- <a style="margin: 20px auto auto 10px; " mce_style="margin: 20px auto auto 10px; ">
- <asp:Image ID="Image1" runat="server" ImageUrl='<%#Eval("ImageUrlPlotos") %>' Width="301"
- Height="181" valign="top" />
- </a>
- <div class="we8">
- <b>
- <%#Eval("wineHeaderName")%></b> <a href="../wineparty.aspx?wineHeaderID=<%#Eval(" mce_href="wineparty.aspx?wineHeaderID=<%#Eval("WinePartyHeaderId") %>" class="we8" style=" text-align:right;">查看详情</a><br />
- <b>酒会时间</b>:
- <%#DataBinder.Eval(Container.DataItem, "ActivityDate", "{0:yyyy-mm-dd}")%>
- 参加人数:
- <%#Eval("Quantity")%>人以上<br />
- <b>介绍</b>:<%#Eval("Description")%></div>
- <br />
- </div>
- <div class="wenzi" style="width: 560px; height: 275px; float: left;">
- <asp:Repeater ID="Repeater2" runat="server">
- <ItemTemplate>
- <div style="float: left; width: 128px; height: 204px; margin: 20px auto auto 10px;">
- <div align="center" style="border: 1px solid #CCCCCC; background: #FFFFFF;" mce_style="border: 1px solid #CCCCCC; background: #FFFFFF;">
- <img src='<%#Eval("ImageUrl")%>' alt='<%#Eval("Name")%>' /></div>
- <div style="background-color: #F9F7F7; text-align: center; color: #333333;" mce_style="background-color: #F9F7F7; text-align: center; color: #333333;">
- <%# Eval("Name") %></div>
- </div>
- </ItemTemplate>
- </asp:Repeater>
- </div>
- </div>
- </ItemTemplate>
- </asp:Repeater>
- </div>
下面是后台代码的绑定外层Repeater数据方法:
[c-sharp] view plaincopyprint?
-
-
-
- protected void GetBind()
- {
- Repeater1.DataSource = BindData();
- Repeater1.DataBind();
- }
-
-
-
-
- protected IList BindData()
- {
- using (RoalDragonBoatDADataContext ctx = new RoalDragonBoatDADataContext())
- {
- var query_WineParty = from query_wineHeader in ctx.Nop_WinePartyHeader
- join query_winePlotos in ctx.Nop_WinePartyPhotos
- on query_wineHeader.WinePartyHeaderId equals query_winePlotos.WinePartyHeaderId
-
-
-
-
-
-
- join query_wineplace in ctx.Nop_WinePartyPlace
- on query_wineHeader.WinePartyHeaderId equals query_wineplace.WinePartyHeaderId
- select new
- {
- query_winePlotos.WinePartyPhotosId,
- query_wineHeader.WinePartyHeaderId,
- wineHeaderName = query_wineHeader.Name,
- query_wineplace.Quantity,
- ImageUrlPlotos = query_winePlotos.ImageUrl == null ? "" : "../" + query_winePlotos.ImageUrl,
- query_winePlotos.Description,
- query_wineHeader.ActivityDate,
-
-
- };
- return query_WineParty.ToList();
- }
- }
下面是绑定内嵌Repeater数据集方法:
[c-sharp] view plaincopyprint?
-
-
-
-
-
- protected void Repeater1_ItemDataBound(object sender, RepeaterItemEventArgs e)
- {
- if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
- {
- Repeater rpColumnNews = (Repeater)e.Item.FindControl("Repeater2");
- string[] newrowv = e.Item.DataItem.ToString().Split(new char[] { ',' });
-
- string wineHeaderID = Convert.ToString(newrowv[1]);
- int index = wineHeaderID.LastIndexOf("=");
- rpColumnNews.DataSource = GetNop_WinePartyProduct(int.Parse(wineHeaderID.Substring(index + 1)));
- rpColumnNews.DataBind();
- }
- }
-
-
-
-
- protected IList GetNop_WinePartyProduct(int winePartyHeaderID)
- {
- using (RoalDragonBoatDADataContext ctx = new RoalDragonBoatDADataContext())
- {
- var query = (from query_winePartyProduct in ctx.Nop_WinePartyProduct
- join query_Product in ctx.Nop_Product
- on query_winePartyProduct.ProductId equals query_Product.ProductId
- join query_product_picture in ctx.Nop_ProductPicture
- on query_Product.ProductId equals query_product_picture.ProductID
- where query_winePartyProduct.WinePartyHeaderId == winePartyHeaderID
- select new
- {
- query_Product.Name,
- ImageUrl = PictureManager.GetPictureUrl(query_product_picture.PictureID, 200, false)
- }).Take(4);
- return query.ToList();
- }
- }
绑定数据方法,大同小异,大家可以根据自己的需求更改所需方法;
顺便把Repeater分页方法也粘贴出来吧。希望对大家有帮助!
[c-sharp] view plaincopyprint?
- protected void Page_Load(object sender, EventArgs e)
- {
- if (!IsPostBack)
- {
- labPage.Text = "1";
- contrlRepeater();
- }
- }
-
- public void contrlRepeater()
- {
-
-
-
- PagedDataSource pds = new PagedDataSource();
- pds.DataSource = BindData();
- pds.AllowPaging = true;
- pds.PageSize = 3;
- pds.CurrentPageIndex = labPage.Text == "" ? 1 : Convert.ToInt32(this.labPage.Text) - 1;
- LabCountPage.Text = pds.PageCount.ToString();
- labPage.Text = (pds.CurrentPageIndex + 1).ToString();
- this.lbtnpritPage.Enabled = true;
- this.lbtnFirstPage.Enabled = true;
- this.lbtnNextPage.Enabled = true;
- this.lbtnDownPage.Enabled = true;
- if (pds.CurrentPageIndex < 1)
- {
- this.lbtnpritPage.Enabled = false;
- this.lbtnFirstPage.Enabled = false;
- }
- if (pds.CurrentPageIndex == pds.PageCount - 1)
- {
- this.lbtnNextPage.Enabled = false;
- this.lbtnDownPage.Enabled = false;
- }
- Repeater1.DataSource = pds;
- Repeater1.DataBind();
- BindDropList();
- }
-
-
-
-
-
- protected void lbtnFirstPage_Click(object sender, EventArgs e)
- {
- this.labPage.Text = "1";
- DropDownList1.SelectedValue = this.labPage.Text;
- this.contrlRepeater();
- }
-
-
-
-
-
- protected void lbtnpritPage_Click(object sender, EventArgs e)
- {
- if (labPage.Text != "")
- {
- this.labPage.Text = Convert.ToString(Convert.ToInt32(labPage.Text) - 1);
- DropDownList1.SelectedValue = this.labPage.Text;
- }
- this.contrlRepeater();
- }
-
-
-
-
-
- protected void lbtnNextPage_Click(object sender, EventArgs e)
- {
- if (labPage.Text != "")
- {
- this.labPage.Text = Convert.ToString(Convert.ToInt32(labPage.Text) + 1);
- DropDownList1.SelectedValue = this.labPage.Text;
- }
- this.contrlRepeater();
- }
-
-
-
-
-
- protected void lbtnDownPage_Click(object sender, EventArgs e)
- {
- this.labPage.Text = this.LabCountPage.Text;
- DropDownList1.SelectedValue = this.labPage.Text;
- this.contrlRepeater();
- }