1

我在该转发器(Repeater1)的项目模板中有一个转发器(Repeater1),一个超链接和一个转发器(Repeater2)。然后repeater2也包含一个超链接。Repeater1 的超链接用于类别,Repeater2 的超链接用于其子类别。

如图所示是代码

<ul class="categories" id="categoryheader">
                <li id="categoryItem">
                    <h4>Categories</h4>
                    <ul class="categories" id="categorylist">
            <asp:Repeater ID="repCategories" runat="server" OnItemDataBound="repCategories_ItemDataBound">
                <HeaderTemplate>
                    <ul>
                </HeaderTemplate>
                <ItemTemplate>
                    <li>
                        <asp:HyperLink ID="hyperCategories" runat="server"><%#Eval("CategoryName")%></asp:HyperLink>
                                <ul class="subcategories" id="subcategorylist">
                                    <asp:Repeater ID="repsubcategory" runat="server">
                                        <HeaderTemplate>
                                            <ul>
                                        </HeaderTemplate>
                                        <ItemTemplate>
                                            <li>
                                                <asp:HyperLink ID="hyperSubCategories" runat="server"><%#Eval("SubCategoryName")%></asp:HyperLink>                                       
                                            </li>
                                        </ItemTemplate>
                                        <FooterTemplate></ul></FooterTemplate>
                                    </asp:Repeater>
                                </ul>
                            </li>
                </ItemTemplate>
                <FooterTemplate></ul></FooterTemplate>
            </asp:Repeater>
        </ul>
                </li>

并且类别和子类别表如下->

ProductCategory
Column Name     Data Type       Constraint
CategoryID      Varchar(20)     Primary key
CategoryName    Varchar(100)    Unique key
Description     Varchar(1000)   

ProductSubCategory
Column Name        Data Type    Constraint
SubCategoryID      Int          Auto increment , Primary key
SubCategoryName    Varchar(100) 
Description        Varchar(1000)    
CategoryID         Varchar(20)  Foreign key with Product Category

我已使用此查询将中继器绑定到数据库

{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            DataSet ds = new ViewAction().GetAllProductCategoryData();
            repCategories.DataSource = ds;
            repCategories.DataBind();
            DataSet ds1 = new ViewAction().GetAllCompanyInfoData();
            repCompany.DataSource = ds1;
            repCompany.DataBind();

            //DataSet ds2 = new ViewAction().GetAllProductSubCategoryData();
            //repsubcategory.DataSource = ds2;
            //repsubcategory.DataBind();
        }
    }
    protected void ItemBound(object sender, RepeaterItemEventArgs args)
    {
        if (args.Item.ItemType == ListItemType.Item)
        {
            Repeater childRepeater = (Repeater)args.Item.FindControl("repsubcategory");
            var item = args.Item as RepeaterItem;
            SqlCommand cmd = DataConnection.GetConnection().CreateCommand();
            cmd.CommandText = "Select ProductSubCategory.CategoryID,ProductSubCategory.SubCategoryName,ProductCategory.CategoryID from ProductSubCategory right join ProductCategory on ProductSubCategory.CategoryID=@hypercategory";
            SqlDataAdapter da = new SqlDataAdapter(cmd);
            DataSet ds = new DataSet();
            da.Fill(ds);
            cmd.Dispose();


            childRepeater.DataSource = ds;
            //childRepeater.DataSourceID = "CategoryID";
            childRepeater.DataBind();
            DataConnection.CloseConnection();

        }
    }
    protected void repCategories_ItemDataBound(object sender, RepeaterItemEventArgs e)
    {
        RepeaterItem item = e.Item;
        if ((item.ItemType == ListItemType.Item) ||
        (item.ItemType == ListItemType.AlternatingItem))
        {
            Repeater repsubcategory = (Repeater)item.FindControl("repsubcategory");
            string hyperCategories = hyperCategory.Text;
            DataRowView drv = (DataRowView)item.DataItem;
            SqlCommand cmd = DataConnection.GetConnection().CreateCommand();
            cmd.CommandText = "Select ProductSubCategory.CategoryID,ProductSubCategory.SubCategoryName,ProductCategory.CategoryID from ProductSubCategory right join ProductCategory on ProductSubCategory.CategoryID=ProductCategory.CategoryID";
            SqlDataAdapter da = new SqlDataAdapter(cmd);
            DataSet ds = new DataSet();
            da.Fill(ds);
            cmd.Dispose();


            repsubcategory.DataSource = ds;
            //childRepeater.DataSourceID = "CategoryID";
            repsubcategory.DataBind();
            DataConnection.CloseConnection();
        }
    }
}  

告诉我查询是错误的

cmd.CommandText = "Select ProductSubCategory.CategoryID,ProductSubCategory.SubCategoryName,ProductCategory.CategoryID from ProductSubCategory right join ProductCategory on ProductSubCategory.CategoryID=@hypercategory";

它将获取每个类别的所有子类别。

请告诉我正确的查询,以便我可以根据两个超链接的值操作此查询。

4

0 回答 0