Control study-> Custom DataGrid flip control (sample code download)

Source: Internet
Author: User

(1) Description

Generally, you can use the page flip button that comes with the DataGrid to perform all page flip operations, but sometimes you want to customize your page flip style.

Or beautify the display effect, you can write a paging user control.

(2). display the effect dashboard

Note: Some controls on the DataGrid are custom flip controls.

(2)Code

1. *. aspx code file:

<% @ Page Language = "C #" codebehind = "dgcustomerpagination. aspx. cs" autoeventwireup = "false" inherits = "new_datagrid custom paging event. 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 ms_positioning = "gridlayout">
<Form ID = "form1" method = "Post" runat = "server">
<Asp: DataGrid id = "datagrid1" style = "Z-INDEX: 101; left: 96px; position: absolute; top: 160px"
Runat = "server" width = "768px" allowpaging = "true">
<Header style backcolor = "#999900"> <Pagerstyle nextpagetext = "next page" prevpagetext = "Previous Page" mode = "numericpages"> </pagerstyle>
</ASP: DataGrid> <asp: linkbutton id = "lblastpage" style = "Z-INDEX: 105; left: 808px; position: absolute; top: 120px"
Onclick = "changepageclick" runat = "server" commandargument = "last"> last page </ASP: linkbutton> <asp: linkbutton id = "lbnextpage" style = "Z-INDEX: 104; left: 744px; position: absolute; top: 120px"
Onclick = "changepageclick" runat = "server" commandargument = "Next"> next page </ASP: linkbutton> <asp: linkbutton id = "lbprevpage" style = "Z-INDEX: 103; left: 680px; position: absolute; top: 120px"
Onclick = "changepageclick" runat = "server" commandargument = "Prev"> previous page </ASP: linkbutton> <asp: linkbutton id = "lbfirstpage" style = "Z-INDEX: 102; left: 616px; position: absolute; top: 120px"
Onclick = "changepageclick" runat = "server" commandargument = "first"> last page </ASP: linkbutton>
<Asp: Label id = "lbldisplayposition" style = "Z-INDEX: 106; left: 240px; position: absolute; top: 120px"
Runat = "server" width = "64px"> </ASP: Label>
<Asp: textbox id = "txtselectpage" style = "Z-INDEX: 107; left: 360px; position: absolute; top: 120px"
Runat = "server" width = "pixel PX"> </ASP: textbox>
<Asp: button id = "btnselectpage" style = "Z-INDEX: 108; left: 472px; position: absolute; top: 120px"
Runat = "server" text = "OK"> </ASP: button>
<Asp: Label id = "lblerror" style = "Z-INDEX: 109; left: 520px; position: absolute; top: 120px"
Runat = "server" width = "88px"> label </ASP: Label>
<Asp: Label id = "lbtotal" style = "Z-INDEX: 110; left: pixel PX; position: absolute; top: 120px" runat = "server"
Width = "136px"> </ASP: Label> </form>
</Body>
</Html>

 

2. *. aspx. CS code file

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;

Namespace new_datagrid custom paging event
{
/// <Summary>
/// DataGrid custom page
/// </Summary>
Public class webform1: system. Web. UI. Page
{
Protected system. Web. UI. webcontrols. DataGrid datagrid1;
Protected system. Web. UI. webcontrols. linkbutton lblastpage;
Protected system. Web. UI. webcontrols. linkbutton lbnextpage;
Protected system. Web. UI. webcontrols. linkbutton lbprevpage;
Protected system. Web. UI. webcontrols. linkbutton lbfirstpage;
Protected system. Web. UI. webcontrols. Label lbldisplayposition;
Protected system. Web. UI. webcontrols. Button btnselectpage;
Protected system. Web. UI. webcontrols. textbox txtselectpage;
Protected system. Web. UI. webcontrols. Label lblerror;
Protected system. Web. UI. webcontrols. Button button1;

Protected system. Web. UI. webcontrols. Label lbtotal; // displays the total number of records.
Private Static int intrecord = 0; // displays the total number of records.

Private void page_load (Object sender, system. eventargs E)
{
This. lblerror. Text = "";
If (! Page. ispostback)
{
Getdata (); // generate data
This. binddata (); // bind data to the DataGrid
Displaystats (); // display the following representation: 2/100 indicates a total of 100 pages. Currently, this is the second page.
}
}

//


// generate data
///
private void getdata ()
{< br> datatable table = new datatable ();
datacolumncollection columns = table. columns;

// Define the structure
Columns. Add ("student ID", typeof (system. int32 ));
Columns. Add ("score", typeof (system. int32 ));

Random myrandom = new random ();
Int intscore = 0;
For (INT I = 1; I <101; I ++)
{
Intscore = myrandom. Next (100 );
Datarow drnew = table. newrow ();
Drnew [0] = I. tostring ();
Drnew [1] = intscore. tostring ();
Table. Rows. Add (drnew );
}
This. viewstate. Add ("table", table );

Intrecord = table. Rows. count;
This. lbtotal. Text = "Total:" + intrecord. tostring () + "record ";
}

/// <Summary>
/// Bind the DataGrid
/// </Summary>
Private void binddata ()
{
Datatable table = (datatable) This. viewstate ["table"];
This. Maid = table;
This. datagrid1.databind ();

}

/// <Summary>
/// Custom control button event
/// </Summary>
/// <Param name = "sender"> </param>
/// <Param name = "E"> </param>
Public void changepageclick (Object sender, eventargs E)
{
String commandname = (linkbutton) sender). commandargument. tostring ();
Switch (commandname)
{
Case "Next ":
If (maid-1)
{
Datagrid1.currentpageindex + = 1;
}
Break;
Case "Prev ":
If (maid> 0)
{
Datagrid1.currentpageindex-= 1;
}
Break;
Case "last ":
Datagrid1.currentpageindex = (datagrid1.pagecount-1 );
Break;
Default:
Datagrid1.currentpageindex = 0;
Break;
}
Displaystats ();
}

Public void displaystats ()
{< br> This. lbldisplayposition. TEXT = (maid + 1 ). tostring () + "/";
This. lbldisplayposition. text + = maid. tostring ();
this.txt selectpage. TEXT = (maid + 1 ). tostring ();
}

# 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. Maid + = new system. Web. UI. webcontrols. Maid pageindexchanged );
This. btnselectpage. Click + = new system. eventhandler (this. btnselectpage_click );
This. Load + = new system. eventhandler (this. page_load );

}
# Endregion

Private void datagrid1_pageindexchanged (Object source, system. Web. UI. webcontrols. datagridpagechangedeventargs E)
{
Try
{
This. datagrid1.currentpageindex = E. newpageindex;
}
Catch
{
This. Maid = 0;
}
This. binddata ();
}

Private void btnselectpage_click (Object sender, system. eventargs E)
{
Int intpage = 0;
Try
{
Intpage = int.parse(this.txt selectpage. Text );
}
Catch
{
This. lblerror. Text = "only numbers can be entered! ";
Return;
}

Try
{
This. datagrid1.currentpageindex = intPage-1;
This. binddata ();
}
Catch
{
This. Maid = 0;
This. binddata ();
This. lblerror. Text = "beyond the page number range! ";
}

Displaystats ();
}
}
}

(4). ExampleSource codeDownload (run directly)

Http://www.cnblogs.com/Files/ChengKing/Custom split page example.rar

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.