ASP.NET中repeater嵌套實現代碼(附源碼)

來源:互聯網
上載者:User

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,資源下載

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.