zoukankan      html  css  js  c++  java
  • 不一样的repeater嵌套

    以前做repeater嵌套数据绑定,

    最典型的例子莫过于显示部门 然后在每个部门下显示所有员工 的例子。

    你要问怎么做 好 百度一下,网上告诉你的例子都是 先给外面的repeater绑定部门数据,然后在部门的itemtemplate里再放repeater

    然后再部门repeater 的OnItemDataBound里绑定对应的部门数据

    like this: 

    DepBll是业务层类 已经写好的函数 用于根据部门编号获取对应员工信息 等。

    protected void departs_ItemDataBound(object sender, RepeaterItemEventArgs e)
        {
            if (e.Item.ItemIndex != -1)
            {
                DepBLL bll = new DepBLL();
                Repeater emps = e.Item.FindControl("departs") as Repeater;
                int depid = int.Parse( (e.Item.FindControl("depid") as Label).Text);
                emps.DataSource = bll.selectEmpByDep(depid);
                emps.DataBind();
            }
        }
    

    上面那种的具体实现过程就不讲了,地球人都知道 网上搜一搜就可以了。 今天这里说的是另外一种方法,

    链接:http://support.microsoft.com/default.aspx?scid=kb;en-us;306154

    不用OnItemDataBound事件,直接绑定(前提当然 肯定 绝对 必须 是要用到那种 一对多 的持久化层映射)

    以前用的是nhibernate ,现在更好了有entityFrameWork,实体层一句代码不用写

    实体层代码参考

    departInfo中对员工表映射实体的定义(vs2008自动生成的):

    /// <summary>
            /// 架构中不存在 empInfo 的注释。
            /// </summary>
            [global::System.Data.Objects.DataClasses.EdmRelationshipNavigationPropertyAttribute("BosloyERPModel", "FK__empInfo__departI__2EDAF651", "empInfo")]
            [global::System.Xml.Serialization.XmlIgnoreAttribute()]
            [global::System.Xml.Serialization.SoapIgnoreAttribute()]
            [global::System.Runtime.Serialization.DataMemberAttribute()]
            public global::System.Data.Objects.DataClasses.EntityCollection<empInfo> empInfo
            {
                get
                {
                    return ((global::System.Data.Objects.DataClasses.IEntityWithRelationships)(this)).RelationshipManager.GetRelatedCollection<empInfo>("BosloyERPModel.FK__empInfo__departI__2EDAF651", "empInfo");
                }
                set
                {
                    if ((value != null))
                    {
                        ((global::System.Data.Objects.DataClasses.IEntityWithRelationships)(this)).RelationshipManager.InitializeRelatedCollection<empInfo>("BosloyERPModel.FK__empInfo__departI__2EDAF651", "empInfo", value);
                    }
                }
            }
    

    在页面repeater控件嵌套的时候可以这样绑定,

    页面上其他代码我就不贴了,只贴数据绑定的代码:

    关键部分( <asp:Repeater ID="employees" DataSource='<%# ((BosloyERPModel.departInfo)Container.DataItem).empInfo %>'  runat="server">)

    <asp:Repeater  ID="departs" runat="server" 
                                onitemdatabound="departs_ItemDataBound">
                            <ItemTemplate><tr>
                            <td>
                            <a onclick="showdialog(<%#Eval("id") %>)"   >
                            <%#  Eval("departName") %>
                            </a>
                            </td>
                            
                            <td  style="padding-left:0px;" colspan="6">
                            <table width="100%">
                            <asp:Repeater ID="employees" DataSource='<%# ((BosloyERPModel.departInfo)Container.DataItem).empInfo %>'  runat="server">
                            <ItemTemplate>
                            <tr>
      
        <td    width="14%"         >
        
        <a onclick="showdialog2(<%#Eval("id") %>)"   ><%#Eval("truename") %></a>
        
         </td>
        <td    width="14%"         > <%# Eval("gender")==null||(bool)Eval("gender")==false?"女":"男" %></td>
        <td    width="14%"         > <%# Eval("birthday")==null?0:(DateTime.Today.Year - ((DateTime)Eval("birthday")).Year)%></td>
        <td    width="14%"         > 职位</td>
        <td    width="14%"         > <%#Eval("mobilephone") %></td>
        <td    width="14%"         > 入职时间</td>
      </tr>
                            </ItemTemplate>
                            </asp:Repeater>
                            </table>
                            </td>
                            </tr></ItemTemplate>
                            </asp:Repeater>
    

    在后台直接绑定部门数据就行了,当然要联动载入员工数据(include(“empinfo”)),就像在nhiernate里设置lazy=false 一样。

    departs.DataSource = db.empInfo.Include("departinfo").ToList(); ;
                departs.DataBind();
    

    最终效果:

  • 相关阅读:
    毛皮立方体
    APPLE buSinEss
    #4613. problem C
    #4614. problem B
    idiots
    熊猫(i)
    圆盘自动机 cell
    小L的占卜
    有趣的数(number)
    「JOISC 2015 Day 1」卡片占卜
  • 原文地址:https://www.cnblogs.com/assassinx/p/1829619.html
Copyright © 2011-2022 走看看