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

Source: Internet
Author: User

(1). Display Effect

(2). Code

1. *. aspx File Code

<% @ Page Language = "C #" codebehind = "webform1.aspx. cs" autoeventwireup = "false" inherits = "new_datagrid custom page button. webform1" %>
<! Doctype HTML public "-// W3C // dtd html 4.0 transitional // en">
<HTML>
<Head>
<Title> webform1 </title>
<Meta name = "generator" content = "Microsoft Visual Studio. NET 7.1">
<Meta name = "code_language" content = "C #">
<Meta name = "vs_defaultclientscript" content = "JavaScript">
<Meta name = "vs_targetschema" content = "http://schemas.microsoft.com/intellisense/ie5">
</Head>
<Body ms_positioning = "gridlayout">
<Form ID = "form1" method = "Post" runat = "server">
<Font face = ""> </font>
<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>
</Form>
</Body>
</Html>

2. *. 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;

Namespace new_datagrid custom page button
{
/// <Summary>
/// DataGrid custom page button
/// </Summary>
Public class webform1: system. Web. UI. Page
{
Protected system. Web. UI. webcontrols. DataGrid datagrid1;


Private void page_load (Object sender, system. eventargs E)
{
If (! Page. ispostback)
{
Getdata (); // generate data
This. binddata (); // bind data to the DataGrid
}
}

/// <Summary>
/// Generate data
/// </Summary>
Private void getdata ()
{
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 );
}

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

# 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. Load + = new system. eventhandler (this. page_load );
This. Maid + = new maid (maid itemcreated );

}
# 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 datagrid1_itemcreated (Object sender, datagriditemeventargs E)
{
System. Web. UI. webcontrols. listitemtype elemtype = E. Item. itemtype;
If (elemtype = system. Web. UI. webcontrols. listitemtype. pager)
{
Tablecell pager = (tablecell) E. Item. controls [0];
For (INT I = 0; I <pager. Controls. Count; I ++ = 2)
{
Object o = pager. controls [I];
If (O is linkbutton)
{
Linkbutton H = (linkbutton) O;
H. Text = "" + H. Text + "";
}
Else
{
Label L = (Label) O;
L. Text = string. Format ("<font color = 'red'> [page {0}] </font>", L. Text );
}
}
}
}
}

}

(3) download the source code (you can directly run it after creating a virtual directory)

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.