1.A,运行效果图
1.B,源代码(主要代码摘要)
/App_Code/DBConnection.cs
/App_Code/CategoryInfo.cs
复制代码 代码如下:
using System.Collections.Generic;
public class CategoryInfo
{
int categoryid;
string categoryname;
string categorydesc;
IListArticleInfo> articles;
/// summary>
/// 1,子嵌套数据
/// /summary>
public IListArticleInfo> Articles
{
get { return articles; }
set { articles = value; }
}
public int Categoryid
{
get { return categoryid; }
set { categoryid = value; }
}
public string Categoryname
{
get { return categoryname; }
set { categoryname = value; }
}
public string Categorydesc
{
get { return categorydesc; }
set { categorydesc = value; }
}
public CategoryInfo()
{
}
public CategoryInfo(int categoryid, string categoryname, string categorydesc,IListArticleInfo> articles)
{
this.categoryid = categoryid;
this.categoryname = categoryname;
this.categorydesc = categorydesc;
this.articles = articles;
}
}
/App_Code/ArticleInfo.cs
/App_Code/CategoryOper.cs
复制代码 代码如下:
using System.Data;
using System.Data.SqlClient;
using System.Collections.Generic;
public class CategoryOper
{
public static IListCategoryInfo> SelectAll()
{
IListCategoryInfo> allcate = new ListCategoryInfo>();
string sql = "select category.categoryid,categoryname,categorydesc,id,title,author from category inner join article on category.categoryid=article.categoryid order by category.categoryid";
SqlConnection con = new DBConnection().Con;
SqlCommand com = new SqlCommand();
com.Connection = con;
com.CommandText = sql;
com.CommandType = CommandType.Text;
con.Open();
SqlDataReader sdr = com.ExecuteReader();
int tempcategoryid=0;
CategoryInfo cate=null;
while (sdr.Read())
{
int categoryid=sdr.GetInt32(0);
//如果类别改变则创建一个新的 cate 对象
if(categoryid!=tempcategoryid)
{
cate = new CategoryInfo(sdr.GetInt32(0), sdr.GetString(1), sdr.GetString(2), new ListArticleInfo>());
allcate.Add(cate);
tempcategoryid = categoryid; //把新类别编号付给标识
}
ArticleInfo art = new ArticleInfo(sdr.GetInt32(3), sdr.GetString(4), sdr.GetString(5));
cate.Articles.Add(art);
}
con.Close();
return allcate;
}
public CategoryOper()
{
//
// TODO: 在此处添加构造函数逻辑
//
}
}
/App_Code/ArticleOper.cs
,6
/Default.aspx
复制代码 代码如下:
%@ 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>
/Default.aspx.cs
复制代码 代码如下:
using System;
public partial class _Default : System.Web.UI.Page
{
private void BindCategory()
{
RepCate.DataSource = CategoryOper.SelectAll();
RepCate.DataBind();
}
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
BindCategory();
}
}
}
/web.config
1.C,资源下载
您可能感兴趣的文章:- ASP.Net中数据展示控件的嵌套使用示例
- ASP.NET中Form表单不可以嵌套使用
- asp.net实现DataList与Repeater嵌套绑定的方法
- 在ASP.NET 2.0中操作数据之二十六:排序自定义分页数据
- 在ASP.NET 2.0中操作数据之二十七:创建自定义排序用户界面
- 在ASP.NET 2.0中操作数据之二十八:GridView里的Button
- 在ASP.NET 2.0中操作数据之二十九:用DataList和Repeater来显示数据
- 在ASP.NET 2.0中操作数据之三十:格式化DataList和Repeater的数据
- 在ASP.NET 2.0中操作数据之三十一:使用DataList来一行显示多条记录
- 在ASP.NET 2.0中操作数据之三十二:数据控件的嵌套