0

ネストされた Repeater コントロールを使用して、カテゴリとそれに対応するサブカテゴリのリストを正常に表示しました。

.aspx

<asp:Repeater ID="rMainCategories" runat="server" OnItemDataBound="rMainCategories_ItemDataBound">
    <ItemTemplate>
        <li>
            <asp:Label ID="lblCategoryName" runat="server"
                Text='<%# Eval("MainCategory") %>' />
        </li>
        <ul>
            <asp:Repeater ID="rSubCategories" runat="server">
                <ItemTemplate>
                    <li>
                        <asp:HyperLink ID="hlProductName" runat="server" Text='<%# Eval("SubCategory")%>' />
                    </li>
                </ItemTemplate>
            </asp:Repeater>
        </ul>
    </ItemTemplate>
</asp:Repeater>

.aspx.cs

void GetCategories()
{
    con.Open();
    SqlCommand cmd = new SqlCommand();
    cmd.Connection = con;
    cmd.CommandText = "SELECT m.MainCatID, m.Category AS MainCategory " +
        "FROM MainCategories m WHERE m.MainCatID " +
        "IN (SELECT MainCatID FROM SubCategories) " +
        "SELECT s.SubCatID, s.Category AS SubCategory, s.MainCatID FROM SubCategories s";
    SqlDataAdapter da = new SqlDataAdapter(cmd);
    DataSet ds = new DataSet();
    da.Fill(ds);

    ds.Relations.Add(new DataRelation("CategoriesRelation", ds.Tables[0].Columns["MainCatID"],
        ds.Tables[1].Columns["MainCatID"]));
    rMainCategories.DataSource = ds.Tables[0];
    rMainCategories.DataBind();
    con.Close();
}

protected void rMainCategories_ItemDataBound(object sender, RepeaterItemEventArgs e)
{
    if (e.Item.ItemType == ListItemType.Item ||
    e.Item.ItemType == ListItemType.AlternatingItem)
    {
        DataRowView drv = e.Item.DataItem as DataRowView;
        Repeater rSubCategories = e.Item.FindControl("rSubCategories") as Repeater;
        rSubCategories.DataSource = drv.CreateChildView("CategoriesRelation");
        rSubCategories.DataBind();
    }
}

出力

サブカテゴリを含むメインカテゴリのリスト

主なカテゴリー一覧

主なカテゴリのリスト

サブカテゴリなしでカテゴリを表示する方法はありますか? お知らせ下さい。ありがとうございました。

4

2 に答える 2

0

このようにしてみてください:

この方法でメインカテゴリをリピーターにバインドすることをお勧めしますGetCategories()

public Void GetCategories()
{
rMainCategories.datasource=dt ;      // select * from MainCategories
rMainCategories.DataBind();

}

データを取得して保存するSession[subcategory]

select * from subcategories where MainCatid in 
(select MainCad ID from Categories)

そして、DataBound メソッドでサブカテゴリをバインドします

protected void rMainCategories_ItemDataBound(object sender, RepeaterItemEventArgs e)
{
    if (e.Item.ItemType == ListItemType.Item ||
    e.Item.ItemType == ListItemType.AlternatingItem)
    {
        DataRowView drv = e.Item.DataItem as DataRowView;
        Repeater rSubCategories = e.Item.FindControl("rSubCategories") as Repeater;
        // save MainCatID in rMainCategories
        Label lblMainCatID=(Label)e.Item.FindControl(lblMainCatID);

     DataTable subcat=(DataTable)Session[subcategory];

    rSubCategories.DataSource = dt;        // filter subcate with MainCatID 
    rSubCategories.DataBind();
}
}
于 2014-11-01T11:09:23.697 に答える