不知道园子里面有没有类似的文章,不过这是我自己研究所得,翻来覆去想想还是粘贴出来吧。毕竟是自己血汗啊(这么热的天)!
Repeater嵌套问题,可以简单的理解为双重for循环。原理其实是一样的,下面是我在项目中摸索的,编译通过,效果明显。呵呵, 好了
源码如下:(前台)
1 <div style=" 924px; height: 278px; line-height:20px;">
2 <asp:Repeater ID="Repeater1" runat="server" OnItemDataBound="Repeater1_ItemDataBound">
3 <ItemTemplate>
4 <div class="wenzi" style="background: #F9F7F7; 924px; height: 293px; border: 1px dashed #CCCCCC;
5 margin: auto; margin-top: 8px; border-top: #FC89A1 solid 1px;">
6 <div style=" 350px; height: 275px; float: left; margin:20px auto auto 10px;">
7 <a style="margin: 20px auto auto 10px; " mce_style="margin: 20px auto auto 10px; ">
8 <asp:Image ID="Image1" runat="server" ImageUrl='<%#Eval("ImageUrlPlotos") %>' Width="301"
9 Height="181" valign="top" />
10 </a>
11 <div class="we8">
12 <b>
13 <%#Eval("wineHeaderName")%></b> <a href="../wineparty.aspx?wineHeaderID=<%#Eval(" mce_href="wineparty.aspx?wineHeaderID=<%#Eval("WinePartyHeaderId") %>" class="we8" style=" text-align:right;">查看详情</a><br />
14 <b>酒会时间</b>:
15 <%#DataBinder.Eval(Container.DataItem, "ActivityDate", "{0:yyyy-mm-dd}")%>
16 参加人数:
17 <%#Eval("Quantity")%>人以上<br />
18 <b>介绍</b>:<%#Eval("Description")%></div>
19 <br />
20 </div>
21 <div class="wenzi" style=" 560px; height: 275px; float: left;">
22 <asp:Repeater ID="Repeater2" runat="server">
23 <ItemTemplate>
24 <div style="float: left; 128px; height: 204px; margin: 20px auto auto 10px;">
25 <div align="center" style="border: 1px solid #CCCCCC; background: #FFFFFF;" mce_style="border: 1px solid #CCCCCC; background: #FFFFFF;">
26 <img src='<%#Eval("ImageUrl")%>' alt='<%#Eval("Name")%>' /></div>
27 <div style="background-color: #F9F7F7; text-align: center; color: #333333;" mce_style="background-color: #F9F7F7; text-align: center; color: #333333;">
28 <%# Eval("Name") %></div>
29 </div>
30 </ItemTemplate>
31 </asp:Repeater>
32 </div>
33 </div>
34 </ItemTemplate>
35 </asp:Repeater>
36 </div>
下面是后台代码的绑定外层Repeater数据方法:
/// <summary>
/// 绑定Repeater数据源
/// </summary>
protected void GetBind()
{
Repeater1.DataSource = BindData();
Repeater1.DataBind();
}
///// <summary>
/// 绑定数据源
/// </summary>
/// <returns></returns>
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_winePartyProduct in ctx.Nop_WinePartyProduct
//on query_wineHeader.WinePartyHeaderId equals query_winePartyProduct.WinePartyHeaderId
//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
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,
//query_Product.Name,
//ImageUrlPicture = PictureManager.GetPictureUrl(query_product_picture.PictureID, 200, false)
};
return query_WineParty.ToList();
}
}
下面是绑定内嵌Repeater数据集方法:
/// <summary>
/// 在项被数据绑定后 , 绑定内嵌Repeater
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
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[] { ',' });
//提取ID 格式:——>wineHeaderID=1 截取字符串
string wineHeaderID = Convert.ToString(newrowv[1]);
int index = wineHeaderID.LastIndexOf("=");
rpColumnNews.DataSource = GetNop_WinePartyProduct(int.Parse(wineHeaderID.Substring(index + 1)));
rpColumnNews.DataBind();
}
}
/// <summary>
/// 绑定内嵌Repeater数据源
/// </summary>
/// <returns></returns>
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分页方法也粘贴出来吧。希望对大家有帮助!
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
labPage.Text = "1";
contrlRepeater();
}
}
//Repeater分页控制显示方法
public void contrlRepeater()
{
//pb pb1 = new pb();
//DataSet ss = new DataSet();
//ss = pb1.returnDs("select top 200 [标题],[时间] from news");
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();
}
/// <summary>
/// 首页
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
protected void lbtnFirstPage_Click(object sender, EventArgs e)
{
this.labPage.Text = "1";
DropDownList1.SelectedValue = this.labPage.Text;
this.contrlRepeater();
}
/// <summary>
/// 上一页
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
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();
}
/// <summary>
/// 下一页
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
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();
}
/// <summary>
/// 末页
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
protected void lbtnDownPage_Click(object sender, EventArgs e)
{
this.labPage.Text = this.LabCountPage.Text;
DropDownList1.SelectedValue = this.labPage.Text;
this.contrlRepeater();
}
效果图如下:
所有核心代码如上。编译通过,效果正常,如果不当之处,希望大家多多指教。