http://blog.163.com/liuyangxin_99/blog/static/64028122200858102655439/

 

Repeater控件双重绑定

前台代码:

<asp:Repeater ID="parentRepeater" runat="server" OnItemDataBound="parentRepeater_ItemDataBound" Visible=false>

            <ItemTemplate>

                <b>一级:

                    <%# DataBinder.Eval(Container.DataItem,"categorytitle") %>

                </b>       

                <br>

                <!-- 子Repeater开始 -->

                <asp:Repeater ID="childRepeater" runat="server">

                    <ItemTemplate>

                        二级:

                        <%# DataBinder.Eval(Container.DataItem, "newstitle")%>

                        <br>

                    </ItemTemplate>

                </asp:Repeater>

                <!-- 子Repeater结束 -->

            </ItemTemplate>

        </asp:Repeater>

后台代码:

protected void Page_Load(object sender, EventArgs e)

    {

        SqlDataAdapter sda = new SqlDataAdapter("select * from newscategory", con);

        DataSet ds = new DataSet();

        sda.Fill(ds, "table1");

        DataTable dt = new DataTable();

        DataView dv = new DataView(ds.Tables["table1"]);

}

    protected void parentRepeater_ItemDataBound(object sender, RepeaterItemEventArgs e)

    {

        if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)

        {

            Repeater r2 = (Repeater)e.Item.FindControl("childRepeater");

            //找到分类Repeater关联的数据项

            DataRowView rowv = (DataRowView)(e.Item.DataItem);

            //提取分类ID

            string CategorieId = Convert.ToString(rowv["categoryid"]);

            //根据分类ID查询该分类下的产品,并绑定产品Repeater

            string sql = "select distinct top 6 newstitle from newscontent,newscategory where newscontent.categoryid = '" + CategorieId + "'";

            SqlDataAdapter sda = new SqlDataAdapter(sql, con);

            DataSet ds = new DataSet();

            sda.Fill(ds, "table2");

            r2.DataSource = ds.Tables["table2"].DefaultView;

            r2.DataBind();

        }  

    }

 

 posted on 2010-06-05 15:38  ddxgc  阅读(365)  评论(0编辑  收藏  举报