"Go" ASP. DataList nested method to realize category navigation of level two menu

Source: Internet
Author: User

Just started to learn ASP . NET to do the site, do is an e-book purchase site, found that books have multilevel category.  For example: Fiction classification under the World Classics, Chinese Classical fiction .... Literature has a literary theory of Chinese classical poetry. These if directly to die in the site, not conducive to the back to add and modify the classification, this is very detrimental to the post-site maintenance. So what is the way to classify data two through the background input, the front desk to read it? That is, these classifications exist in the database, enabling dynamic reads.

Find some information, and then I integrated, finally can be applied to their own website. Use a Listview or DataList nesting. I use my book website as an example here.   implement level two nesting with DataList.

Implementation Principleis inpageloadload First class classification under events, then in the first class categoryDataList1of theOnitemdatabound events go down to read the first class classificationID,then according to the first class classificationIDto retrieve the level two category.ID(foreign key), when the level two categoryID(equal to) one-level classificationIDto bind data in a database to a class twoDatalist2on.

Look first.

A Database design

1. CNC Library Design

Typeone ( Primary Classification database table )

ID int PRIMARY KEY

Booktypeone Char First class classification name

Typertwo (Level Two classification database table)

ID int PRIMARY KEY

BookType char level two classification name

Idno int First Class class name (foreign key)

Bookinfo Book Information table

Bookid int PRIMARY KEY

BookType Char foreign key

Two front-desk aspx code

First nesting of two DataList in the foreground aspx page

Code:

<asp:datalist id= "DataList1" runat= "Server"

onitemdatabound= "Datalist1_itemdatabound" >

<ItemTemplate>

<asp:label id= "Label1" runat= "Server" visible= "false"

text= '<%# Eval ("ID") %>'/>

<asp:label id= "Booktypeonelabel" runat= "Server"

text= '<%# Eval ("Booktypeone") %>'/>

<asp:datalist id= "DataList2" runat= "Server" >

<ItemTemplate>

<asp:label id= "BookTypeOneLabe2" runat= "Server"

text= '<%# Eval ("BookType") %>'/>

</ItemTemplate>

</asp:DataList>

</ItemTemplate>

</asp:DataList>

three. Backstage code: When the page loads, he wants to load first class category

protected void Page_Load (object sender, EventArgs e)

{

Try

{

SqlDataReader Read;

String sql = "Select *from typeone";

DB db = new db ();

SqlConnection CNN = db. ConnectionCnnString1;

Read = db. selecttable (SQL);

Datalist1.datasource = read;

Datalist1.databind ();

}

catch (Exception ex)

{

Response.Write ("Query failed" + ex.) Message);

}

Finally

{

}

This is the code for the datalist1 datalist1_itemdatabound event.

Never write a direct binding in page-load .

protected void Datalist1_itemdatabound (object sender, DataListItemEventArgs e)

{

SqlDataReader Read;

if (E.item.itemtype = = ListItemType.Item | | e.item.itemtype = = listitemtype.alternatingitem)

{

DataList MyDataList1 = (DataList) e.item.findcontrol ("DataList2");

Extract first-level category ID

String id= ((Label) E.item.findcontrol ("Label1")). Text;

The ID of the first Class category matches the level two classification Idno (foreign key)

String sql = "Select BookType from Typertwo where typertwo.idno=" + ID;

Upload SQL statements to DB class queries

DB db = new db ();

SqlConnection CNN = db. ConnectionCnnString1;

Read = db. selecttable (SQL);

Mydatalist1.datasource = read;

Mydatalist1.databind ();

}

}

Four. Database DB.cs class Code

declaring connection Properties

Public SqlConnection ConnectionCnnString1

{

Get

{

String constr = configurationmanager.connectionstrings["ConnectionString"]. ConnectionString;

CNN = new SqlConnection (CONSTR);

Cnn. Open ();

Return CNN;

}

}

Query method, this method only needs to receive the SQL statements sent over

Public SqlDataReader selecttable (String sql)

{

if (CNN. state = = connectionstate.closed)

{

Cnn. Open ();

}

cmd = new SqlCommand (sql, CNN);

Read = cmd. ExecuteReader ();

return read;

}

Here to play a role in the hope that the great God can fools, perfect.

"Go" ASP. DataList nested method to realize category navigation of level two menu

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.