. Simple paging effect with DataGrid implemented in net

Source: Internet
Author: User
datagrid| Paging
<!--front page-->

<%@ Page language= "C #" codebehind= "WebForm1.aspx.cs" autoeventwireup= "false" inherits= "Try1.webform1"%>
<! DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 transitional//en" >
<HTML>
<HEAD>
<title>WebForm1</title>
<meta content= "Microsoft Visual Studio. NET 7.1" name= "generator" >
<meta content= "C #" Name= "Code_language" >
<meta content= "JavaScript" name= "vs_defaultClientScript" >
<meta content= "http://schemas.microsoft.com/intellisense/ie5" name= "Vs_targetschema" >
</HEAD>
<body>
<form id= "Form1" method= "POST" runat= "Server" >
<p><font face= "Song Body" ></FONT> </P>
<P>
<asp:datagrid id= "DATAGRID1" runat= "Server" bordercolor= "#3366CC" borderstyle= "None" borderwidth= "1px"
Backcolor= "White" cellpadding= "4" allowpaging= "true" pagesize= "5" allowsorting= "true" >
<selecteditemstyle font-bold= "True" forecolor= "#CCFF99" backcolor= "#009999" ></SelectedItemStyle>
<itemstyle forecolor= "#003399" backcolor= "white" ></ItemStyle>
<footerstyle forecolor= "#003399" backcolor= "#99CCCC" ></FooterStyle>
<pagerstyle horizontalalign= "left" forecolor= "#003399" backcolor= "#99CCCC" pagebuttoncount= "6"
Mode= "NumericPages" ></PagerStyle>
</asp:DataGrid></P>
<P>
<asp:panel id= "Panel1" runat= "Server" >
<asp:linkbutton id= "btnprevious" runat= "Server" Commandname= "Previous" ><=Previous</asp:LinkButton>
<font face= "Song Body" >
<asp:linkbutton id= "Btnnext" runat= "Server" Commandname= "Next" >Next=></asp:LinkButton>
<asp:textbox id= "Txtindex" runat= "Server" width= "24px" columns= "2" maxlength= "2" ></asp:TextBox>
<asp:button id= "Btngo" runat= "Server" backcolor= "Gainsboro" borderwidth= "1px" borderstyle= "Solid"
Bordercolor= "Silver" text= "go!" ></asp:Button>
<asp:label id= "Lblerror" runat= "Server" forecolor= "Red" visible= "False" >no that page!</asp:label></ Font>
</asp:Panel></P>
<font face= "Song Body" ></FONT>
<asp:panel id= "Pnlindex" runat= "Server" >Panel</asp:Panel>
</form>
</body>
</HTML>



-------Background Code---------------------------------
The database used by-------is the sqlserver2000 sample database------
Using System;
Using System.Collections;
Using System.ComponentModel;
Using System.Configuration;
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;
Namespace Try1
{
<summary>
Summary description of the WebForm1.
</summary>
public class WebForm1:System.Web.UI.Page
{
protected System.Web.UI.WebControls.DataGrid DataGrid1;
Private DataSet Dsnorth;
protected System.Web.UI.WebControls.Panel Panel1;
protected System.Web.UI.WebControls.LinkButton btnprevious;
protected System.Web.UI.WebControls.LinkButton btnnext;

Use DataView to achieve sort effects
Private DataView Dvnorth;
protected System.Web.UI.WebControls.TextBox Txtindex;
protected System.Web.UI.WebControls.Button Btngo;
protected System.Web.UI.WebControls.Label Lblerror;
protected System.Web.UI.WebControls.Panel Pnlindex;


private void Page_Load (object sender, System.EventArgs e)
{
Place user code here to initialize page
Loaddataset ();
Initpnlindex ();
}
Code generated #region the Web forms Designer
Override protected void OnInit (EventArgs e)
{
//
CodeGen: This call is required for the ASP.net Web forms Designer.
//
InitializeComponent ();
Base. OnInit (e);
}

<summary>
Designer supports required methods-do not use the Code editor to modify
The contents of this method.
</summary>
private void InitializeComponent ()
{
This. Datagrid1.pageindexchanged + = new System.Web.UI.WebControls.DataGridPageChangedEventHandler (this. datagrid1_pageindexchanged);
This. Datagrid1.sortcommand + = new System.Web.UI.WebControls.DataGridSortCommandEventHandler (this. Datagrid1_sortcommand);
This.btnPrevious.Click + = new System.EventHandler (this. NavigationButtonClick);
This.btnNext.Click + = new System.EventHandler (this. NavigationButtonClick);
This.btnGo.Click + = new System.EventHandler (This.btngo_click);
This. Load + = new System.EventHandler (this. Page_Load);
}
#endregion

Loading datasets
private void Loaddataset ()
{
SqlConnection cnnorth=new SqlConnection (configurationsettings.appsettings["cnnorth.connectionstring"));
String cmdstring= "SELECT * from Customers";
SqlDataAdapter Adnorth=new SqlDataAdapter (Cmdstring,cnnorth);
Cnnorth.open ();
Dsnorth=new DataSet ();
Try
{
Adnorth.fill (Dsnorth, "North");
}
Catch
{
Throw
}
Finally
{
Cnnorth.close ();
}
Dvnorth=new DataView ();
Dvnorth.table=dsnorth.tables["North"];
Datagrid1.datasource=dvnorth;
Datagrid1.databind ();
}

Initializing indexes
private void Initpnlindex ()
{
PnlIndex.Controls.Clear ();
int PageCount;
LinkButton Btnindex;
for (pagecount=0;pagecount<datagrid1.pagecount;pagecount++)
{
int btntext=pagecount+1;
Btnindex=new LinkButton ();
Btnindex.text=btntext.tostring ();
Btnindex.commandname=pagecount.tostring ();
Btnindex.click+=new EventHandler (this. Indexbuttonclick);
PNLINDEX.CONTROLS.ADD (Btnindex);
PNLINDEX.CONTROLS.ADD (New LiteralControl (""));
}
}

Index Click Effect
private void Indexbuttonclick (Object Sender,system.eventargs e)
{
String direction= ((LinkButton) sender). CommandName;
int Pageindex=convert.toint32 (direction);
Datagrid1.currentpageindex=pageindex;
Datagrid1.databind ();
}

Sorting effect implementation
private void Datagrid1_sortcommand (object source, System.Web.UI.WebControls.DataGridSortCommandEventArgs e)
{
This.dvnorth.sort=e.sortexpression;
Datagrid1.databind ();
}

Index Click Effect
private void Datagrid1_pageindexchanged (object source, System.Web.UI.WebControls.DataGridPageChangedEventArgs e)
{
Datagrid1.currentpageindex=e.newpageindex;
Datagrid1.databind ();
}

Page effect
private void NavigationButtonClick (object sender, System.EventArgs e)
{
String direction= ((LinkButton) sender). CommandName;
switch (direction. ToUpper ())
{
Case "PREVIOUS":
Datagrid1.currentpageindex=math.max (datagrid1.currentpageindex-1,0);
Break
Case "NEXT":
Datagrid1.currentpageindex=math.min (datagrid1.currentpageindex+1,datagrid1.pagecount-1);
Break
Default
Break
}
Datagrid1.databind ();

}

Go to page effect
private void Btngo_click (object sender, System.EventArgs e)
{
int pageindex=0;
Try
{
Pageindex=convert.toint32 (Txtindex.text);

}
catch (Exception ex)
{
Lblerror.visible=true;
Lblerror.text=ex. message;
}
Finally
{
Lblerror.visible=false;
Lblerror.text= "No that page!";
}
if (Pageindex>0&&pageindex<=datagrid1.pagecount)
{
Try
{
Datagrid1.currentpageindex=math.min (datagrid1.pagecount,pageindex-1);
Datagrid1.databind ();
}
Catch
{
Lblerror.visible=true;

}
Finally
{
Lblerror.visible=false;
}
}
Else
{
Lblerror.visible=true;
}
}

}
}




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.