ASP使用转发器显示具有相似类别的子类别

时间:2013-05-20 15:14:56

标签: asp.net sql html-lists repeater

我正在尝试为每个“类别”构建一个包含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。

1 个答案:

答案 0 :(得分: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