2013-05-20 46 views
0

我正尝试为每个“类别”构建一个带有div的大菜单。在每个div中,H3都基于我从SQL表中提取的“类别”。根据将是一个列表项目是子类别为每个类别...这是链接。 在表中有一堆类别项目。ASP使用中继器显示类似类别的子类别

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

这里是我的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]"> 

当我现在运行这个它(显然)会为每个子类别VS一个div一个div为每个类别。

+0

这里有两个屏幕截图,一个是上面提供的代码,另一个是它应该看起来像。呈现:http://blog.thespikeranch.com/wp-content/uploads/2013/05/looksLike.jpg应该看起来像:http://blog.thespikeranch.com/wp-content/uploads/2013/05/ShouldLookLike .jpg – TheRanch

+0

你的问题似乎有文字缺失?即:“在每个div中,[WORD MISSING?]基于” –

+0

@ShaiCohen True,我的描述有一个html h3标签,因此变为空白。 “H3在每个div中都是基于”应该是什么。 – TheRanch

回答

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 
相关问题