C#--web Data Paging

Source: Internet
Author: User

To implement data paging operations within a data table:

Web page source data:

<%@ page language= "C #" autoeventwireup= "true" codefile= "Car Fenye.aspx.cs" inherits= "Car__fenye"%>

<! DOCTYPE html>

<meta http-equiv= "Content-type" content= "text/html; Charset=utf-8 "/>
<title></title>
<style type= "Text/css" >
#idd {
Font-weight:bold;
Background-color:navy;
Color:black;
Text-align:center;
}
. CLA {
Font-weight:bold;
Background-color:white;
Color:green;
Text-align:center;
}
</style>
<body>
<form id= "Form1" runat= "Server" >
<div>

<asp:repeater id= "Repeater1" runat= "Server" >
<HeaderTemplate>
<table width= "100%" border= "1" cellpadding= "5" cellspacing= "1" id= "IDD" >
<tr>
&LT;TD width= "5%" > Code </td>
&LT;TD width= "20%" > Name </td>
&LT;TD width= "10%" > Models </td>
&LT;TD width= "25%" > Time </td>
&LT;TD width= "10%" > Fuel consumption </td>
&LT;TD width= "10%" > Power </td>
&LT;TD width= "10%" > Displacement </td>
&LT;TD width= "10%" > Unit price </td>
</tr>
</HeaderTemplate>
<ItemTemplate>
<tr class= "CLA" >
&LT;TD width= "5%" ><%# Eval ("Code")%></td>
&LT;TD width= "20%" ><%# Eval ("Name")%></td>
&LT;TD width= "10%" ><%# Eval ("Branda")%></td>
&LT;TD width= "25%" ><%# Eval ("Timea")%></td>
&LT;TD width= "10%" ><%# Eval ("Oila")%></td>
&LT;TD width= "10%" ><%# Eval ("Powersa")%></td>
&LT;TD width= "10%" ><%# Eval ("Exa")%></td>
&LT;TD width= "10%" ><%# Eval ("Pricea")%></td>
</tr>
</ItemTemplate>
<FooterTemplate>
</table>
</FooterTemplate>
</asp:Repeater>

</div>
<asp:button id= "Showye" runat= "Server" onclick= "Showye_click" text= "Home"/>
<asp:button id= "Shangye" runat= "Server" onclick= "Shangye_click1" text= "previous page"/>
<asp:button id= "Nextye" runat= "Server" onclick= "Nextye_click1" text= "next page"/>
<asp:button id= "Weiye" runat= "Server" onclick= "Weiye_click" text= "Last"/>
Section <asp:dropdownlist id= "Ddlye" runat= "Server" autopostback= "True" onselectedindexchanged= "Ddlye_ SelectedIndexChanged ">
</asp:DropDownList>
Page &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Altogether <asp:label id= "Zongye" runat= "Server" forecolor= "Red" ></asp:Label>
Page, now in <asp:label id= "Xianye" runat= "Server" forecolor= "Red" ></asp:Label>
page; <br/>
<br/>
AutoPostBack----automatically post back to the server after the content change is selected </form>
</body>

CS Code:

Using System;
Using System.Collections.Generic;
Using System.Linq;
Using System.Web;
Using System.Web.UI;
Using System.Web.UI.WebControls;

public partial class Car__fenye:System.Web.UI.Page
{
Private Const int pagesize=3;//How many data per page
Private Carsdatacontext context = new Carsdatacontext ();
protected void Page_Load (object sender, EventArgs e)
{
if (! IsPostBack)
{
Showcar ();
Fillye ();
Yshu ();

}
}

private void Showcar ()
//{
var qu = context. Car;
Repeater1.datasource = qu;
Repeater1.databind ();
//}
public void Fillye ()
{
int Tiaoshu=context. Car.count ();//The number of bars for all data in a database table
int yeshu= (int) math.ceiling (1.0*tiaoshu/pagesize);
According to the size of pagesize, to be paged, a page with pagesize data, divided Yeshu pages
for (int i = 0; i < Yeshu; i++)
{

ListItem li = new ListItem ((i + 1). ToString (), i.tostring ());
Ddlye. Items.Add (LI);
}//Add data to DropDownList statically
}
public void Yshu ()
{
int dye = Convert.ToInt32 (Ddlye. SelectedValue);//page 1
int shutiao = dye * pagesize;//The number of pages in front of the total data
var qu = context. Car.skip (Shutiao). Take (pagesize);//paging, Skip takes out all previous bars, shows the number of bars owned by this page
Repeater1.datasource = qu;
Repeater1.databind ();


Zongye. Text = Ddlye. Items.Count.ToString ();//Total number of pages
Xianye. Text = (Ddlye. selectedindex+1). ToString ();//Now on page
//response.write (dye);
//int AAA = Ddlye. SelectedIndex;
//response.write (AAA);
}
protected void Ddlye_selectedindexchanged (object sender, EventArgs e)
{
Yshu ();
}
protected void Showye_click (object sender, EventArgs e)
{
Ddlye. SelectedIndex = 0;
Yshu ();
}
protected void Weiye_click (object sender, EventArgs e)
{
Ddlye. SelectedIndex = Ddlye. items.count-1;
Yshu ();
}

protected void Nextye_click1 (object sender, EventArgs e)
{
if (Ddlye. SelectedIndex < Ddlye. Items.count-1)
{
Ddlye. selectedindex++;
Yshu ();
}
}
protected void Shangye_click1 (object sender, EventArgs e)
{
if (Ddlye. SelectedIndex > 0)
{
Ddlye. selectedindex--;
Yshu ();
}
}
}


Select Top 9 Code from Car;
Select Top 3 * from car where Code not in (select Top 9 Code from car);

Select Ceiling (1.0*count (*)/3) from Car;

C#--web Data Paging

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.