1 View Code 2 3 using System.Collections.Generic; 4 public class CategoryInfo 5 { 6 int categoryid; 7 string categoryname; 8 string categorydesc; 9 IList<ArticleInfo> articles; 10 11 /// <summary> 12 /// 1,子嵌套数据 13 /// </summary> 14 public IList<ArticleInfo> Articles 15 { 16 get { return articles; } 17 set { articles = value; } 18 } 19 20 public int Categoryid 21 { 22 get { return categoryid; } 23 set { categoryid = value; } 24 } 25 26 public string Categoryname 27 { 28 get { return categoryname; } 29 set { categoryname = value; } 30 } 31 32 public string Categorydesc 33 { 34 get { return categorydesc; } 35 set { categorydesc = value; } 36 } 37 38 public CategoryInfo() 39 { 40 41 } 42 43 public CategoryInfo(int categoryid, string categoryname, string categorydesc,IList<ArticleInfo> articles) 44 { 45 this.categoryid = categoryid; 46 this.categoryname = categoryname; 47 this.categorydesc = categorydesc; 48 this.articles = articles; 49 } 50 }
1 View Code 2 3 using System.Data; 4 5 using System.Data.SqlClient; 6 using System.Collections.Generic; 7 public class CategoryOper 8 { 9 public static IList<CategoryInfo> SelectAll() 10 { 11 IList<CategoryInfo> allcate = new List<CategoryInfo>(); 12 string sql = "select category.categoryid,categoryname,categorydesc,id,title,author from category inner join article on category.categoryid=article.categoryid order by category.categoryid"; 13 14 SqlConnection con = new DBConnection().Con; 15 SqlCommand com = new SqlCommand(); 16 com.Connection = con; 17 com.CommandText = sql; 18 com.CommandType = CommandType.Text; 19 20 con.Open(); 21 SqlDataReader sdr = com.ExecuteReader(); 22 int tempcategoryid=0; 23 CategoryInfo cate=null; 24 while (sdr.Read()) 25 { 26 int categoryid=sdr.GetInt32(0); 27 28 //如果类别改变则创建一个新的 cate 对象 29 if(categoryid!=tempcategoryid) 30 { 31 cate = new CategoryInfo(sdr.GetInt32(0), sdr.GetString(1), sdr.GetString(2), new List<ArticleInfo>()); 32 allcate.Add(cate); 33 tempcategoryid = categoryid; //把新类别编号付给标识 34 } 35 36 ArticleInfo art = new ArticleInfo(sdr.GetInt32(3), sdr.GetString(4), sdr.GetString(5)); 37 cate.Articles.Add(art); 38 } 39 con.Close(); 40 return allcate; 41 42 } 43 public CategoryOper() 44 { 45 // 46 // TODO: 在此处添加构造函数逻辑 47 // 48 } 49 }
View Code <%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml" > <head runat="server"> <title>无标题页</title> </head> <body> <form id="form1" runat="server"> <div style="text-align:center"> <asp:Repeater ID="RepCate" runat="server"> <HeaderTemplate> <table border="1"> <tr> <td>分类编号</td> <td>分类名称</td> <td>分类描述</td> </tr> </HeaderTemplate> <ItemTemplate> <tr> <td><%#Eval("categoryid") %></td> <td><%#Eval("categoryname") %></td> <td><%#Eval("categorydesc") %></td> </tr> <tr> <td>本类新闻</td> <td colspan="2"> <asp:Repeater ID="RepArticle" runat="server" DataSource='<%#Eval("articles") %>' > <HeaderTemplate> <table border="1" style="background-color:#00FF00;"> <tr> <td>新闻编号</td> <td>新闻标题</td> <td>新闻作者</td> </tr> </HeaderTemplate> <ItemTemplate> <tr> <td><%#Eval("id") %></td> <td> <asp:HyperLink ID="Hl1" runat="server" Text='<%#Eval("title") %>' NavigateUrl='<%#string.Format("ShowArticle.aspx?id={0}",Eval("id") ) %>' ></asp:HyperLink> </td> <td><%#Eval("author") %></td> </tr> </ItemTemplate> <FooterTemplate> </table> </FooterTemplate> </asp:Repeater> </td> </tr> </ItemTemplate> <FooterTemplate> </table> </FooterTemplate> </asp:Repeater> </div> </form> </body> </html>
1 View Code 2 3 using System; 4 5 public partial class _Default : System.Web.UI.Page 6 { 7 private void BindCategory() 8 { 9 RepCate.DataSource = CategoryOper.SelectAll(); 10 RepCate.DataBind(); 11 } 12 protected void Page_Load(object sender, EventArgs e) 13 { 14 if (!IsPostBack) 15 { 16 BindCategory(); 17 } 18 } 19 }