0

我正在尝试为每个“类别”构建一个带有 div 的大菜单。在每个 div 中,H3 基于我从 SQL 表中提取的“类别”。在下面将是一个列表项,这些项是每个类别的子类别......它们是链接。在表中有一堆类别项目。

如何循环显示与该类别关联的类别和子类别?

这是我的html的设置方式:

<asp:Repeater id="dlCategories" runat="server" DataSourceID="LarryColeSub">
         <ItemTemplate>    
        <div class="col_1">
       <h3><%# Eval("Category") %></h3> 
            <ul>            
    <ItemTemplate>
    <li><a id="cmdSubCategory" class="sectioncontentslink" href='default.aspx?rPage=ToolList&subCatID=<%# Eval("SubCategoryID")%>'>
                                <%# Eval("SubCategory") %></a></li>
    </ItemTemplate>
            </ul>
        </div> 
         </ItemTemplate>
    </asp:Repeater>

这是我的 sqlDataSource:

<asp:SqlDataSource ConnectionString="<%$ ConnectionStrings:LarryCole %>" ID="LarryColeSub" runat="server" SelectCommand="SELECT [SubCategoryID],[SubCategory],[Category],[fkCategoryId] FROM [tblSubCategory]">

当我现在运行它时,它(显然)为每个子类别创建一个 div,而为每个类别创建一个 div。

4

1 回答 1

0
    <%@ Control Language="c#" %>
<%@ Import Namespace="System.Data" %>
<%@ Import Namespace="System.Data.SqlClient" %>
<%@ Import Namespace="System.Configuration" %>
<script runat="server">  
    protected void Page_Load(object sender, System.EventArgs e) {  
        if (!Page.IsPostBack) {  
            SqlConnection MyConnection;  
            SqlCommand MyCommand;  
            SqlDataAdapter MyAdapter;  
            DataTable MyTable;  
            DataSet ds;
            ds = new DataSet();

            MyConnection = new SqlConnection();  
            MyConnection.ConnectionString = ConfigurationManager.ConnectionStrings["db_name_here"].ConnectionString;  

            MyCommand = new SqlCommand(); 
            MyCommand.CommandType = CommandType.Text;  
            MyCommand.Connection = MyConnection;


            MyCommand.CommandText = "SELECT * FROM tblSubCategory";  
            MyTable = new DataTable();   
            MyAdapter = new SqlDataAdapter();  
            MyAdapter.SelectCommand = MyCommand;  
            MyAdapter.Fill(ds,"SubCategory");  
            MyCommand.Dispose();

            MyCommand.CommandText = "SELECT * FROM tblCategory";  
            MyTable = new DataTable();  
            MyAdapter = new SqlDataAdapter();  
            MyAdapter.SelectCommand = MyCommand;  
            MyAdapter.Fill(ds,"Category");  
            MyCommand.Dispose();


            ds.Relations.Add("myrelation",ds.Tables["Category"].Columns["CategoryID"], ds.Tables["SubCategory"].Columns["fkCategoryID"]);
            //populate parent repeater
            rpCategories.DataSource = ds.Tables["Category"];  
            rpCategories.DataBind();  

            MyAdapter.Dispose();  
            MyConnection.Dispose();  

        }  
    }  
</script>  

//html repeater code
    <asp:Repeater id="rpCategories" runat="server">
             <ItemTemplate>    
            <div class="col_1">
                <h3><%# DataBinder.Eval(Container.DataItem,"Category") %></h3>
                <ul>    
                    <asp:Repeater id="rpSubCategories" runat="server" datasource='<%# ((DataRowView)Container.DataItem).Row.GetChildRows("myrelation") %>'>
                     <ItemTemplate>
                         <li><a id="cmdSubCategory" class="sectioncontentslink" href='default.aspx?varName=BlahBlah&subCatID=<%# ((DataRow)Container.DataItem)["SubCategoryID"] %>'>
                              <%# ((DataRow)Container.DataItem)["SubCategory"] %></a>
                         </li>
                     </ItemTemplate>
                    </asp:Repeater>
                </ul>
            </div> 
             </ItemTemplate>
        </asp:Repeater
于 2013-06-10T17:18:02.550 回答