webForm中数据绑定控件的嵌套

  有时候不得不在数据绑定控件中嵌套数据绑定控件来完成要求的数据显示效果 .公司在做一个商城的项目,有个页面的要求如图所示.

    我很使劲儿的想了想,貌似只能嵌套数据控件了.外部的控件(简称为Repeater1)直接指定DataSource就可以了,但是内部数据控件(Repeater2)的DataSource的在父控件每次迭代时都需要变化,就不是单纯的指定一下就可以的事了.
 我有很使劲儿的想了想, 数据绑定控件貌似都有一个ItemDataBound事件,每次迭代数据前都会触发,只要在Repeater1的ItemDataBound事件中, 按照当前迭代的Item,指定Repeater2的DataSource就可以了.
 可以通过e.Item.ItemType判断当前迭代的是哪一项(Item或者HeaderItem或其他.)
 e.Item.FindControl("[id]")可以获得指定的Repeater1中的控件.
 e.Item.DataItem可以得到当前项的数据.可以是DataRow,也可以是一个类的实体.

 

演示的代码如下:

先定义两个用于展示数据的实体:

/// <summary>
    /// 订单实体类
    /// </summary>
    public class ModelOrder
    {
        public string OrderId { get; set; }
        public string OrderNum { get; set; }
        public string OrderTime { get; set; }
        public string OrderPrice { get; set; }
        public string ShouPeople { get; set; }
        public string OrderState { get; set; }
    }
    /// <summary>
    /// 物品实体类
    /// </summary>
    public class ModelGood
    {
        public string OrderId { get; set; }
        public string GoodImg { get; set; }
        public string GoodName { get; set; }
        public string GoodType { get; set; }
        public string GoodPrice { get; set; }
        public string GoodQuantity { get; set; }
    }


 

aspx页面:

<asp:Repeater ID="rpOrder" runat="server" OnItemDataBound="rpOrder_ItemDataBound">
                <ItemTemplate>
                    <table border="0" cellpadding="0" cellspacing="0" class="OrderTable">
                        <tr>
                            <td width="268" height="31" class="t1" colspan="3">
                                <div class="p_box">
                                    <p class="p1">
                                        订单编号:<%#Eval("OrderNum") %></p>
                                    <p class="p1">
                                        下单时间:<%#Eval("OrderTime") %></p>
                                    <p class="p1">
                                        订单金额:<span class="redBold"><%#Eval("OrderPrice") %></span></p>
                                    <p class="p2">
                                        收货人:<%#Eval("ShouPeople") %></p>
                                    <div class="clear">
                                    </div>
                                </div>
                            </td>
                        </tr>
                        <tr>
                            <td width="546" class="brt">
                                <table border="0" cellpadding="0" cellspacing="0" class="OrderTable_child">
                                    <asp:Repeater ID="rpGoods" runat="server">
                                        <ItemTemplate>
                                            <tr>
                                                <td width="72" height="90">
                                                    <div class="imgBox">
                                                        <img src='<%#Eval("GoodImg") %>' /></div>
                                                </td>
                                                <td width="196" class="brt">
                                                    <div class="a1">
                                                        <a href="#">
                                                            <%#Eval("GoodName") %></a></div>
                                                </td>
                                                <td width="96" class="brt">
                                                    <%#Eval("GoodType") %>
                                                </td>
                                                <td width="92" class="brt">
                                                    <%#Eval("GoodPrice") %>
                                                </td>
                                                <td>
                                                    <%#Eval("GoodQuantity") %>
                                                </td>
                                            </tr>
                                        </ItemTemplate>
                                    </asp:Repeater>
                                </table>
                            </td>
                            <td width="186" class="brt">
                                <%#Eval("OrderState") %>
                            </td>
                            <td width="81">
                                <p class="P_style">
                                    <a href="#">付款</a></p>
                                <p class="P_style">
                                    <a href="#">查看订单</a></p>
                                <p class="P_style">
                                    <a href="#">取消订单</a></p>
                            </td>
                        </tr>
                    </table>
                </ItemTemplate>
            </asp:Repeater>


 

在OnPageLoad中模拟数据,并且绑定Repeater1的数据源:

protected void Page_Load(object sender, EventArgs e)
        {
           
            //用于展示数据
            listOrder.Add(new ModelOrder()
                {
                    OrderId = "1",
                    OrderNum = "123456",
                    OrderTime = "2012-10-26 8:40",
                    OrderPrice = "384:00",
                    ShouPeople = "张小三",
                    OrderState = "手机出库,待收货付款"
                });
            listOrder.Add(new ModelOrder()
                {
                    OrderId = "2",
                    OrderNum = "123456",
                    OrderTime = "2012-10-26 8:40",
                    OrderPrice = "384:00",
                    ShouPeople = "张小三",
                    OrderState = "手机出库,待收货付款"
                });
            

            //用于展示数据
            listGood.Add(new ModelGood() { 
                OrderId = "1",
                GoodImg = @"images/goods_50x50.jpg",
                GoodName ="移动电源5200mAh,LED显示灯",
                GoodType = "普通",
                GoodPrice = "199.00",
                GoodQuantity = "1"
            });
            listGood.Add(new ModelGood()
            {
                OrderId = "1",
                GoodImg = @"images/goods_50x50.jpg",
                GoodName = "移动电源5200mAh,LED显示灯",
                GoodType = "普通",
                GoodPrice = "199.00",
                GoodQuantity = "1"
            });
            listGood.Add(new ModelGood()
            {
                OrderId = "1",
                GoodImg = @"images/goods_50x50.jpg",
                GoodName = "移动电源5200mAh,LED显示灯",
                GoodType = "普通",
                GoodPrice = "199.00",
                GoodQuantity = "1"
            });
            listGood.Add(new ModelGood()
            {
                OrderId = "2",
                GoodImg = @"images/goods_50x50.jpg",
                GoodName = "移动电源5200mAh,LED显示灯",
                GoodType = "普通",
                GoodPrice = "199.00",
                GoodQuantity = "1"
            });
            listGood.Add(new ModelGood()
            {
                OrderId = "2",
                GoodImg = @"images/goods_50x50.jpg",
                GoodName = "移动电源5200mAh,LED显示灯",
                GoodType = "普通",
                GoodPrice = "199.00",
                GoodQuantity = "1"
            });
            rpOrder.DataSource = listOrder;
           rpOrder.DataBind();
        }


 

在Repeater1的ItemDataBound事件中,切换Repeater2的DataSource:

	/// <summary>
        /// 订单Repeater的ItemDataBound事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void rpOrder_ItemDataBound(object sender, RepeaterItemEventArgs e)
        {
            if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
            {
                Repeater rp = e.Item.FindControl("rpGoods") as Repeater;
                //取得与当前RepeaterItem关联的数据项
                ModelOrder modelOrder = e.Item.DataItem as ModelOrder;
                var list = from good in listGood
                           where good.OrderId == modelOrder.OrderId
                           select good;
               
                rp.DataSource = list.ToList<ModelGood>();
                rp.DataBind();
            }
        }


 

  • 0
    点赞
  • 0
    评论
  • 0
    收藏
  • 一键三连
    一键三连
  • 扫一扫,分享海报

©️2020 CSDN 皮肤主题: 大白 设计师:CSDN官方博客 返回首页
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、C币套餐、付费专栏及课程。

余额充值