zoukankan      html  css  js  c++  java
  • Repeater嵌套(灵活的)

    页面代码

    <form id="form1" runat="server">
        <asp:Repeater ID="rptCategories" runat="server" OnItemCommand="rptCategories_ItemCommand">
            <HeaderTemplate>
                <table width="100%" border="1" cellspacing="0" cellpadding="0">
            </HeaderTemplate>
            <ItemTemplate>
                <!--分类名称-->
                <tr>
                    <th>
                        <asp:LinkButton ID="LinkButton1" CommandName='<%#DataBinder.Eval(Container.DataItem, "ID") %>'
                            runat="server"><%#DataBinder.Eval(Container.DataItem, "ID")%></asp:LinkButton>
                            <input type="button" value="收" onclick="aa()" />
                    </th>
                    <th>
                       <label runat="server" id="lbl"> <%# DataBinder.Eval(Container.DataItem, "loginid")%></label>
                    </th>
                </tr>
                <!--分类下的产品-->
                <asp:Repeater ID="rptProduct" runat="server">
                    <ItemTemplate>
                        <tr>
                            <td>
                                <%# DataBinder.Eval(Container.DataItem, "Course")%>
                            </td>
                            <td>
                                <%# DataBinder.Eval(Container.DataItem, "Score")%>
                            </td>
                        </tr>
                    </ItemTemplate>
                </asp:Repeater>
            </ItemTemplate>
            <FooterTemplate>
                </table>
            </FooterTemplate>
        </asp:Repeater>
        </form>

    根据点击Repeater上的按钮,在这行下方嵌套Repeater

     protected void rptCategories_ItemCommand(object source, RepeaterCommandEventArgs e)
            {
               
                if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
                {
                    Repeater rptProduct = (Repeater)e.Item.FindControl("rptProduct");
                    //找到分类Repeater关联的数据项 
                    DataRowView rowv = (DataRowView)e.Item.DataItem;
                    var a = e.Item.Controls[0];
                    //提取分类ID 
                    string id = e.CommandName;
                    //string CategorieId = Convert.ToString(rowv["Name"]);
                    //int CategorieId = Convert.ToInt32(rowv["ID"]);
                    //根据分类ID查询该分类下的产品,并绑定产品Repeater 
                    List<Student> list = new List<Student>();
                    Student stu = new Student() { Course = "语文", Score = 135 };
                    list.Add(stu);
                    list.Add(new Student() { Course = "数学", Score = 141 });
                    //绑定嵌套数据
                    rptProduct.DataSource = list;
                    rptProduct.DataBind();
                }
            }

    绑定原数据

    protected void Page_Load(object sender, EventArgs e)
            {
                if (!IsPostBack)
                {
                    if (!IsPostBack)
                    {
                        List<Student> list = new List<Student>();
                        Student stu = new Student() { ID = 100, Name = "赵六", Sex = "", Phone = "15889898998" };
                        list.Add(stu);
                        list.Add(new Student() { ID = 102, Name = "王五", Sex = "", Phone = "136*******8382" });
                        list.Add(new Student() { ID = 103, Name = "卡琳", Sex = "", Phone = "138*******9083" });
                        list.Add(new Student() { ID = 105, Name = "王五", Sex = "", Phone = "157*******7002" });
    
                        DataTable dt = new DataTable();
                        DataSet ds = GetDataSet("select * from profile");
    
                        rptCategories.DataSource = (DataTable)ds.Tables[0];
                        rptCategories.DataBind();
                    }
                }
            }
    View Code
  • 相关阅读:
    【Beta阶段】第一次Scrum Meeting
    【Beta阶段】第二次Scrum Meeting
    【Beta阶段】第三次Scrum Meeting
    [BUAA软工]Alpha阶段事后分析
    [BUAA软工]Alpha阶段测试报告
    [北航软工]团队贡献分规则
    Windows Server 2008 R2之二从介质安装 AD DS
    Windows Server 2008 R2之一活动目录服务部署
    DC84问
    获取命令行指定参数
  • 原文地址:https://www.cnblogs.com/bit-by-bit/p/4316270.html
Copyright © 2011-2022 走看看