Asp. NET in repeater nesting implementation code (with source) _ Practical skills

Source: Internet
Author: User
Tags eval
1.A, Operation effect Chart

1.B, source code (main code summary)
/app_code/dbconnection.cs
/app_code/categoryinfo.cs
Copy Code code as follows:

Using System.Collections.Generic;
public class CategoryInfo
{
int CategoryID;
String CategoryName;
String Categorydesc;
Ilist<articleinfo> articles;
<summary>
1, sub nested data
</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
Copy Code code as follows:

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 CA Tegory.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);
Create a new Cate object if the category changes
if (Categoryid!=tempcategoryid)
{
Cate = new CategoryInfo (SDR. GetInt32 (0), SDR. GetString (1), SDR. GetString (2), New list<articleinfo> ());
Allcate. ADD (Cate);
Tempcategoryid = CategoryID; Pay the new category number to the identification
}
Articleinfo art = new Articleinfo (SDR). GetInt32 (3), SDR. GetString (4), SDR. GetString (5));
Cate. Articles.add (art);
}
Con. Close ();
return allcate;
}
Public Categoryoper ()
{
//
TODO: Add constructor logic here
//
}
}

/app_code/articleoper.cs
, 6
/default.aspx
Copy Code code as follows:

<%@ 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 ">
<title> Untitled Page </title>
<body>
<form id= "Form1" runat= "Server" >
<div style= "Text-align:center" >
<asp:repeater id= "repcate" runat= "Server" >
<HeaderTemplate>
<table border= "1" >
<tr>
<td> Category Number </td>
<td> Category name </td>
<td> Classification Description </td>
</tr>
</HeaderTemplate>
<ItemTemplate>
<tr>
<td><% #Eval ("CategoryID")%></td>
<td><% #Eval ("CategoryName")%></td>
<td><% #Eval ("Categorydesc")%></td>
</tr>
<tr>
<td> This kind of news </td>
&LT;TD colspan= "2" >
<asp:repeater id= "reparticle" runat= "server" Datasource= ' <% #Eval ("articles")%> ' >
<HeaderTemplate>
<table border= "1" style= "Background-color: #00FF00;" >
<tr>
<td> News Number </td>
<td> News title </td>
<td> News Author </td>
</tr>
</HeaderTemplate>
<ItemTemplate>
<tr>
<td><% #Eval ("id")%></td>
<td>
<asp:hyperlink id= "Hl1" runat= "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>

/default.aspx.cs
Copy Code code as follows:

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, Resource download
Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.