ASP.NET中repeater嵌套實(shí)現(xiàn)代碼(附源碼)
更新時間:2013年03月07日 11:57:40 作者:
repeater嵌套經(jīng)常會在一些特殊效果顯示下會用到,新手朋友們可以詳細(xì)看下本文,希望對你有所幫助,代碼很整潔同時附有源碼
1.A,運(yùn)行效果圖
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,子嵌套數(shù)據(jù)
/// </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);
//如果類別改變則創(chuàng)建一個新的 cate 對象
if(categoryid!=tempcategoryid)
{
cate = new CategoryInfo(sdr.GetInt32(0), sdr.GetString(1), sdr.GetString(2), new List<ArticleInfo>());
allcate.Add(cate);
tempcategoryid = categoryid; //把新類別編號付給標(biāo)識
}
ArticleInfo art = new ArticleInfo(sdr.GetInt32(3), sdr.GetString(4), sdr.GetString(5));
cate.Articles.Add(art);
}
con.Close();
return allcate;
}
public CategoryOper()
{
//
// TODO: 在此處添加構(gòu)造函數(shù)邏輯
//
}
}
/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>無標(biāo)題頁</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>新聞標(biāo)題</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
復(fù)制代碼 代碼如下:
using System.Collections.Generic;
public class CategoryInfo
{
int categoryid;
string categoryname;
string categorydesc;
IList<ArticleInfo> articles;
/// <summary>
/// 1,子嵌套數(shù)據(jù)
/// </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
復(fù)制代碼 代碼如下:
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);
//如果類別改變則創(chuàng)建一個新的 cate 對象
if(categoryid!=tempcategoryid)
{
cate = new CategoryInfo(sdr.GetInt32(0), sdr.GetString(1), sdr.GetString(2), new List<ArticleInfo>());
allcate.Add(cate);
tempcategoryid = categoryid; //把新類別編號付給標(biāo)識
}
ArticleInfo art = new ArticleInfo(sdr.GetInt32(3), sdr.GetString(4), sdr.GetString(5));
cate.Articles.Add(art);
}
con.Close();
return allcate;
}
public CategoryOper()
{
//
// TODO: 在此處添加構(gòu)造函數(shù)邏輯
//
}
}
/App_Code/ArticleOper.cs
,6
/Default.aspx
復(fù)制代碼 代碼如下:
<%@ 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>無標(biāo)題頁</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>新聞標(biāo)題</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
復(fù)制代碼 代碼如下:
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中數(shù)據(jù)展示控件的嵌套使用示例
- ASP.NET中Form表單不可以嵌套使用
- asp.net實(shí)現(xiàn)DataList與Repeater嵌套綁定的方法
- 在ASP.NET 2.0中操作數(shù)據(jù)之二十六:排序自定義分頁數(shù)據(jù)
- 在ASP.NET 2.0中操作數(shù)據(jù)之二十七:創(chuàng)建自定義排序用戶界面
- 在ASP.NET 2.0中操作數(shù)據(jù)之二十八:GridView里的Button
- 在ASP.NET 2.0中操作數(shù)據(jù)之二十九:用DataList和Repeater來顯示數(shù)據(jù)
- 在ASP.NET 2.0中操作數(shù)據(jù)之三十:格式化DataList和Repeater的數(shù)據(jù)
- 在ASP.NET 2.0中操作數(shù)據(jù)之三十一:使用DataList來一行顯示多條記錄
- 在ASP.NET 2.0中操作數(shù)據(jù)之三十二:數(shù)據(jù)控件的嵌套
相關(guān)文章
ajaxControlToolkit中CascadingDropDown的用法說明
今天頭叫寫一個類似三級聯(lián)動的控件,最好實(shí)現(xiàn)無刷新,是石油軟件中的一個數(shù)據(jù),需要表現(xiàn)出類似 X1-22 這樣的格式,上下標(biāo)的數(shù)據(jù)是固定的 想了很多辦法來表現(xiàn)這個數(shù)字,最后決定用3個DropDownList控件2008-11-11
剖析Asp.Net路由系統(tǒng)實(shí)現(xiàn)原理
本篇文章主要介紹了剖析Asp.Net路由系統(tǒng)實(shí)現(xiàn)原理,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2017-02-02
asp.net 純真ip庫取得所在地實(shí)現(xiàn)代碼
asp.net 純真ip庫取得所在地實(shí)現(xiàn)代碼,需要的朋友可以參考一下。2009-05-05
.net indexOf(String.indexOf 方法)
字符串的IndexOf()方法搜索在該字符串上是否出現(xiàn)了作為參數(shù)傳遞的字符串,如果找到字符串,則返回字符的起始位置 (0表示第一個字符,1表示第二個字符依此類推)如果說沒有找到則返回 -12012-10-10
asp.net MVC使用PagedList.MVC實(shí)現(xiàn)分頁效果
這篇文章主要為大家詳細(xì)介紹了asp.net MVC使用PagedList.MVC實(shí)現(xiàn)分頁效果,具有一定的參考價值,感興趣的小伙伴們可以參考一下2017-07-07
ASP.Net WebAPI與Ajax進(jìn)行跨域數(shù)據(jù)交互時Cookies數(shù)據(jù)的傳遞
本文主要介紹了ASP.Net WebAPI與Ajax進(jìn)行跨域數(shù)據(jù)交互時Cookies數(shù)據(jù)傳遞的相關(guān)知識。具有很好的參考價值。下面跟著小編一起來看下吧2017-05-05
Asp.net core利用MediatR進(jìn)程內(nèi)發(fā)布/訂閱詳解
這篇文章主要給大家介紹了關(guān)于Asp.net core利用MediatR進(jìn)程內(nèi)發(fā)布/訂閱的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對大家學(xué)習(xí)或者使用Asp.net core具有一定的參考學(xué)習(xí)價值,需要的朋友們下面來一起學(xué)習(xí)學(xué)習(xí)吧2019-06-06
.NET?Core?實(shí)現(xiàn)緩存的預(yù)熱的方式
本文介紹了緩存預(yù)熱在.NETCore應(yīng)用中的重要性以及如何實(shí)現(xiàn)緩存預(yù)熱,緩存預(yù)熱可以提升用戶體驗(yàn)、降低數(shù)據(jù)庫壓力、提高系統(tǒng)穩(wěn)定性,感興趣的朋友一起看看吧2025-03-03

