前台:
<div id="category_tree">
<asp:repeater ID="Parent" runat="server">
<itemtemplate>
<dl>
<dt><a href='<%# "ParentInfo.aspx?ParentId="+ Eval("Id") %>' style="text-decoration:none;"><%# DataBinder.Eval(Container.DataItem, "CategoryName")%></a></dt>
<asp:repeater ID="Child" DataSource='<%# Eval("myrelation")%>' runat="server">
<itemtemplate>
<dd><a href='<%# "ChildrenInfo.aspx?childrenId="+ Eval("Id") %>' style="text-decoration:none;"><%# DataBinder.Eval(Container.DataItem, "[\"CategoryName\"]")%></a></dd>
</itemtemplate>
</asp:repeater> </dl>
</itemtemplate>
</asp:repeater>
</div>
样式:
#category_tree{border:4px solid #f1faff; background-color:#fff;padding-bottom:13px;padding-top:5px}
#category_tree dl{margin:6px; text-align:left;}
#category_tree dt{background:url(../Content/Images/DefaultImages/lineBg.gif) repeat-x left bottom;
color:#3f3f3f; padding:2px 0 3px 12px;
}
#category_tree dt a{background:url(../Content/Images/DefaultImages/bg.gif) no-repeat 0 -69px;
color:#3f3f3f; padding-left:15px; text-decoration:none;
}
#category_tree dd{padding:3px 0 3px 10px;float:left;width:75px;overflow: hidden; }
#category_tree dd a{color:#404040; text-decoration:none;}
#category_tree dd a:hover{color:#ff6600; text-decoration:none;}
后台:
con = new SqlConnection(sqlCon);
//查询父类
SqlCommand cmd1 = new SqlCommand("select * from CategoryInfo where ParentCateId is null", con);
SqlDataAdapter dad1 = new SqlDataAdapter(cmd1);
DataSet ds = new DataSet();
dad1.Fill(ds, "parentCategory");
//查询子类
SqlCommand cmd2 = new SqlCommand("select * from CategoryInfo where ParentCateId is not null", con);
SqlDataAdapter dad2 = new SqlDataAdapter(cmd2);
dad2.Fill(ds, "ChildCategory");
ds.Relations.Add("myRelation", ds.Tables["parentCategory"].Columns["Id"], ds.Tables["ChildCategory"].Columns["ParentCateId"]);
this.Parent.DataSource=ds;
this.Parent.DataBind();