ASP.NET中repeater嵌套实现代码(附源码)
程序员文章站
2024-03-03 15:30:28
1.a,运行效果图 1.b,源代码(主要代码摘要) /app_code/dbconnection.cs /app_code/categoryinfo.cs 复制...
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;
ilist<articleinfo> articles;
/// <summary>
/// 1,子嵌套数据
/// </summary>
public ilist<articleinfo> 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,ilist<articleinfo> 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 ilist<categoryinfo> selectall()
{
ilist<categoryinfo> allcate = new list<categoryinfo>();
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 list<articleinfo>());
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,资源下载
1.b,源代码(主要代码摘要)
/app_code/dbconnection.cs
/app_code/categoryinfo.cs
复制代码 代码如下:
using system.collections.generic;
public class categoryinfo
{
int categoryid;
string categoryname;
string categorydesc;
ilist<articleinfo> articles;
/// <summary>
/// 1,子嵌套数据
/// </summary>
public ilist<articleinfo> 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,ilist<articleinfo> 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 ilist<categoryinfo> selectall()
{
ilist<categoryinfo> allcate = new list<categoryinfo>();
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 list<articleinfo>());
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,资源下载