Use Relations to establish the relationship between tables, but use the PagedDataSource class to paging DataList.

Source: Internet
Author: User

A three-level linkage example is provided recently. To display the same effect as a tree control on the page, each category has its own subclass. Therefore, Relations is used to establish the relationship between tables (Relations is a set of relationships used to link tables and allow browsing from parent tables to child tables)
PagedDataSource is used to paging DataList (PagedDataSource encapsulates the properties of the DataGrid Control, which enables the control to execute pagination). The built-in pagination of the DataGrid is the class used. Therefore, pagination of a large amount of data is not allowed.

First, create two tables. The SQL statement is as follows:
IndexPage is the primary table, and SePage is the sub table.
If exists (select * from dbo. sysobjects where id = object_id (n' [dbo]. [IndexPage] ') and OBJECTPROPERTY (id, n'isusertable') = 1)
Drop table [dbo]. [IndexPage]
GO

Create table [dbo]. [IndexPage] (
[IndexID] [int] IDENTITY (1, 1) not null,
[IndexName] [varchar] (100) COLLATE Chinese_PRC_CI_AS NULL,
[Content] [varchar] (1000) COLLATE Chinese_PRC_CI_AS NULL
) ON [PRIMARY]
GO

If exists (select * from dbo. sysobjects where id = object_id (n' [dbo]. [SePage] ') and OBJECTPROPERTY (id, n'isusertable') = 1)
Drop table [dbo]. [SePage]
GO

Create table [dbo]. [SePage] (
[SeID] [int] IDENTITY (1, 1) not null,
[IndexID] [int] NULL,
[SeName] [varchar] (100) COLLATE Chinese_PRC_CI_AS NULL,
[Content] [varchar] (1000) COLLATE Chinese_PRC_CI_AS NULL
) ON [PRIMARY]
GO

Stored Procedure: (1) GetIndexPage
Create proc GetIndexPage
AS
SELECT * FROM INDEXPAGE

(2) GetSecondPage
Create proc GetSecondPage
AS
SELECT * FROM SEPAGE

Basic results
HTML code:

<% @ Page language = "c #" Codebehind = "ShowYellowPage. aspx. cs" AutoEventWireup = "false" Inherits = "WLNet. YellowPage. ShowYellowPage" %>
<% @ Import Namespace = "System. Data" %>
<! Doctype html public "-// W3C // dtd html 4.0 Transitional // EN">
<HTML>
<HEAD>
<Title> ShowYellowPage </title>
<Meta content = "Microsoft Visual Studio. NET 7.1" name = "GENERATOR">
<Meta content = "C #" name = "CODE_LANGUAGE">
<Meta content = "JavaScript" name = "vs_defaultClientScript">
<LINK href = "../CSS/BasicLayout.css" type = "text/css" rel = "stylesheet">
<Meta content = "http://schemas.microsoft.com/intellisense/ie5" name = "vs_targetSchema">
</HEAD>
<Body MS_POSITIONING = "GridLayout">
<Form id = "Form1" method = "post" runat = "server">
<Table class = "GbText" style = "BORDER-COLLAPSE: collapse" borderColor = "# cccccc" width = "100%"
Align = "center" border = "1">
<TR>
<TD style = "HEIGHT: 15px" align = "center"> <font style = "FONT-SIZE: 10pt"> total </font>
<Asp: label id = "lbTotalPage" runat = "server"> </asp: label> <font style = "FONT-SIZE: 10pt"> page/Total
<Asp: label id = "lbTotalCount" runat = "server"> </asp: label> Record & nbsp; current </font>
<Asp: label id = "lbCurrentPage" runat = "server"> </asp: label> <font style = "FONT-SIZE: 10pt "> page </font> & nbsp;
<Font style = "FONT-SIZE: 10pt"> jump to </font>
<Asp: dropdownlist id = "ddlPage" runat = "server" Width = "65px" AutoPostBack = "True"> </asp: dropdownlist> <font style = "FONT-SIZE: 10pt "> page </font> </TD>
</TR>
<Tr>
<Td> <asp: datalist id = "dlParent" runat = "server" Width = "100%" RepeatColumns = "2">
<ItemStyle Font-Size = "X-Small"> </ItemStyle>
<ItemTemplate>
<A href = '<% # "ShowData. aspx? IndexID = "+ DataBinder. Eval (Container," DataItem. IndexID ") %> '>
<% # DataBinder. Eval (Container, "DataItem. IndexName") %>
</A>
<Asp: DataList id = "dlChild" runat = "server" datasource = '<% # (DataRowView) Container. dataItem ). row. getChildRows ("MyRelations") %> '>
<ItemTemplate>
& Nbsp; <a href = '<% # "ShowData. aspx? SeID = "+ DataBinder. eval (Container. dataItem, "[\" SeID \ "]") %> '> <% # DataBinder. eval (Container. dataItem, "[\" SeName \ "]") %> </a>
<Br>
</ItemTemplate>
</Asp: DataList>
</ItemTemplate>
<HeaderStyle Font-Size = "X-Small"> </HeaderStyle>
</Asp: datalist> </td>
</Tr>
<Tr>
<TD align = "center" height = "30"> <B> <asp: hyperlink id = "lnkFirst" runat = "server"> page 1 </asp: hyperlink> & nbsp; <asp: hyperlink id = "lnkPrev" runat = "server"> previous page </asp: hyperlink> & nbsp; & nbsp;
<Asp: hyperlink id = "lnkNext" runat = "server"> next page </asp: hyperlink> & nbsp;
<Asp: HyperLink id = "lnkLast" runat = "server"> last page </asp: HyperLink> </B> </TD>
</Tr>
</Table>
</Form>
</Body>
</HTML>

CS code:

Using System;
Using System. Collections;
Using System. ComponentModel;
Using System. Data;
Using System. Drawing;
Using System. Web;
Using System. Web. SessionState;
Using System. Web. UI;
Using System. Web. UI. WebControls;
Using System. Web. UI. HtmlControls;
Using System. Data. SqlClient;
Using ClassDataBase;
Namespace WLNet. YellowPage
{
/// <Summary>
/// Summary of ShowYellowPage.
/// </Summary>
Public class ShowYellowPage: System. Web. UI. Page
{
Private ClassYPage YPage = new ClassYPage ();
Protected System. Web. UI. WebControls. Label lbTotalPage;
Protected System. Web. UI. WebControls. Label lbTotalCount;
Protected System. Web. UI. WebControls. Label lbCurrentPage;
Protected System. Web. UI. WebControls. DropDownList ddlPage;
Private string strConn = System. Configuration. ConfigurationSettings. etettings ["ConnectionString"]. ToString ();
Protected Int32 CurrentPageNumber = 1;
Protected System. Web. UI. WebControls. HyperLink lnkPrev;
Protected System. Web. UI. WebControls. HyperLink lnkNext;
Protected System. Web. UI. WebControls. DataList dlParent;
Protected System. Web. UI. WebControls. HyperLink lnkFirst;
Protected System. Web. UI. WebControls. HyperLink lnkLast; // initialization, set the current page to 1, always use
Protected int PageSize = 10;
Private void Page_Load (object sender, System. EventArgs e)
{
If (! IsPostBack)
{
BindData ();
}
}

# Code generated by region Web Form Designer
Override protected void OnInit (EventArgs e)
{
//
// CODEGEN: This call is required by the ASP. NET Web form designer.
//
InitializeComponent ();
Base. OnInit (e );
}

/// <Summary>
/// The designer supports the required methods-do not use the code editor to modify
/// Content of this method.
/// </Summary>
Private void InitializeComponent ()
{
This. ddlPage. SelectedIndexChanged + = new System. EventHandler (this. ddlPage_SelectedIndexChanged );
This. Load + = new System. EventHandler (this. Page_Load );

}
# Endregion

Private void BindData ()
{
SqlConnection Conn = new SqlConnection (strConn );
SqlCommand cmd = new SqlCommand ("GetIndexPage", Conn );
Cmd. CommandType = CommandType. StoredProcedure;
SqlDataAdapter da = new SqlDataAdapter ();
Da. SelectCommand = cmd;
DataSet ds = new DataSet ();
Da. Fill (ds, "IndexPage ");

SqlCommand cmd1 = new SqlCommand ("GetSePage", Conn );
Repeated 1.commandtype = CommandType. StoredProcedure;
SqlDataAdapter da1 = new SqlDataAdapter ();
Da1.SelectCommand = cmd1;
Da1.Fill (ds, "SePage ");

Ds. Relations. Add ("MyRelations", ds. Tables ["IndexPage"]. Columns ["IndexID"],
Ds. Tables ["SePage"]. Columns ["IndexID"]);

PagedDataSource PPS = new PagedDataSource ();
Pds. DataSource = ds. Tables ["IndexPage"]. DefaultView;
PPS. AllowPaging = true;
PPS. PageSize = 10;
Int CurrentPageNumber;
Int PageSize = PPS. PageSize;
// Obtain the query parameters from the Page on the current Page
If (Request. QueryString ["Page"]! = Null)
CurrentPageNumber = Convert. ToInt32 (Request. QueryString ["Page"]);
Else
CurrentPageNumber = 1;
Int TotalRecords = ds. Tables [0]. Rows. Count;
Int TotalPages = TotalRecords/PageSize; // obtain the total number of pages (total number of records divided by the number of records on each page)
Int ModePages = TotalRecords % PageSize; // obtain the remainder of the modulo operation (total number of records modulo the number of records on each page)
If (ModePages> 0) // if the modulus is not equal to 0, add 1 to the total number of pages.
{
TotalPages + = 1;
}
If (ModePages = 0) // if the modulus is equal to 0, do nothing (or you do not need to judge)
{
}
If (TotalPages = 0)
{
TotalPages = 1;
}
LbTotalPage. Text = TotalPages. ToString (); // display the total number of pages on the page
LbTotalCount. Text = TotalRecords. ToString (); // displays the total number of records on the page.
PPS. CurrentPageIndex = CurrentPageNumber-1;
LbCurrentPage. Text = CurrentPageNumber. ToString ();

If (CurrentPageNumber = 1)
{
LnkFirst. Enabled = false;
LnkPrev. Enabled = false;
If (TotalPages> 1)
{
LnkNext. Enabled = true;
LnkLast. Enabled = true;
}
Else
{
LnkNext. Enabled = false;
LnkLast. Enabled = false;
}
}
Else
{
LnkPrev. Enabled = true;
LnkFirst. Enabled = true;

If (CurrentPageNumber = TotalPages)
{
LnkNext. Enabled = false;
LnkLast. Enabled = false;
}
Else
{
LnkNext. Enabled = true;
LnkLast. Enabled = true;
}
}

If (! PPS. IsFirstPage)
{
LnkPrev. NavigateUrl = Request. CurrentExecutionFilePath + "? Page = "+ Convert. ToString (CurrentPageNumber-1 );
LnkFirst. NavigateUrl = Request. CurrentExecutionFilePath + "? Page = "+ 1;
}

If (! PPS. IsLastPage)
{
LnkNext. NavigateUrl = Request. CurrentExecutionFilePath + "? Page = "+ Convert. ToString (CurrentPageNumber + 1 );
LnkLast. NavigateUrl = Request. CurrentExecutionFilePath + "? Page = "+ Convert. ToString (TotalPages );
}
DdlPage. Items. Clear (); // Clear the page number to jump to (if not cleared, the records will be added cyclically)
Int PCount = int. Parse (lbTotalPage. Text); // you can obtain the total number of pages.
For (int I = 1; I <= PCount; I ++)
{
DdlPage. Items. Add (I. ToString ());
}
DdlPage. Items. FindByText (CurrentPageNumber. ToString (). Selected = true; // display the current page as the first in the list box
// Assign the PagedDataSource object to the Repeater control
DlParent. DataSource = PPS;
DlParent. DataBind ();

}

Private void ddlPage_SelectedIndexChanged (object sender, System. EventArgs e)
{
Int PageSize = int. Parse (ddlPage. SelectedValue. ToString ());
Server. Transfer ("ShowYellowPage. aspx? Page = "+ PageSize );
BindData ();
}

}
}

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.