Datalist for paging and sorting

Source: Internet
Author: User
Tags try catch

Datalist web server controls display data in a certain format, which can be defined using templates and styles. The datalist control can be used for data in any repeated structure, such as a table. The datalist control can display rows in different la s, such as sorting data by column or row. The datalist control cannot automatically use the data source control's update function and automatic paging or sorting. To use the datalist Control for update, paging, and sorting, you must execute the update task in the written code.

In petshop4.0, a class named customlist attracted our attention. It inherits the datalist class and modifies it to implement the paging function. However, in petshop4.0, the paging function is relatively simple (only the page flip function is available). This article introduces the rewrite idea of the datalist class in petshop4.0 and further studies it.

I,

This is rough. You can rewrite it as needed.

II. Specific rewrite code

Using system;
Using system. collections;
Using system. Collections. Specialized;
Using system. text;
Using system. Text. regularexpressions;
Using system. Web. UI;
Using system. Web. UI. webcontrols;

Namespace customlist ...{

Public class customlist: datalist ...{
// Static Constants
Private string html1 = "<Table cellpadding = 0 cellspacing = 0> <tr> <TD colspan = 13> ";
Protected const string html2 = "</TD> </tr> <tr> ";
Protected const string html4 = "</tr> </table> ";
Private Static readonly RegEx RX = new RegEx (@ "^ & page = d +", regexoptions. Compiled );
Private const string link_prev = "<TD> <a href =? Page = {0 }>& nbsp; previous page </a> </TD> ";
Private const string link_more = "<TD> <a href =? Page = {0}> next page & nbsp; </a> </TD> ";
Private const string link_display = "<TD> <a href =? Page = {0 }>& nbsp; {1} & nbsp; </a> </TD> ";
Private const string key_page = "page ";
Private const string comma = "? ";
Private const string amp = "&";

Protected string emptytext;
Private ilist datasource;
Private int pagesize = 10;
Private int currentpageindex;
Private int itemcount;
 

 

Override public object datasource ...{
Set ...{
// This try catch block is to avoid issues with the Vs. Net designer
// The designer will try and bind a datasource which does not derive from ilist
Try ...{
Datasource = (ilist) value;
Itemcount = datasource. count;
}
Catch ...{
Datasource = NULL;
Itemcount = 0;
}
}
}

Public int pagesize ...{
Get... {return pagesize ;}
Set... {pagesize = value ;}
}

Protected int pagecount ...{
Get... {return (itemcount-1)/pagesize ;}
}

Virtual protected int itemcount ...{
Get... {return itemcount ;}
Set... {itemcount = value ;}
}

Virtual public int currentpageindex ...{
Get... {return currentpageindex ;}
Set... {currentpageindex = value ;}
}

Public String emptytext ...{
Set... {emptytext = value ;}
}

Public void setpage (INT index )...{
Onpageindexchanged (new maid (null, index ));
}

Override protected void onload (eventargs e )...{
If (visible )...{
String page = context. request [key_page];
Int Index = (page! = NULL )? Int. parse (PAGE)-1: 0;
Setpage (INDEX );
}
}

/** // <Summary>
/// Overriden method to control how the page is rendered
/// </Summary>
/// <Param name = "Writer"> </param>
Override protected void render (htmltextwriter writer )...{

// Check there is some data attached
If (itemcount = 0 )...{
Writer. Write (emptytext );
Return;
}

// Mask the query
String query = context. Request. url. query. Replace (comma, AMP );
Query = Rx. Replace (query, String. Empty );

// Write out the first part of the control, the table Header
Writer. Write (html1 );

// Call the inherited method
Base. Render (writer );


// Write out a table row Closure
Writer. Write (html2 );

// List information
Writer. Write ("<TD> total & nbsp;" + pagecount + "& nbsp; Page & nbsp;" + itemcount + "& nbsp item </TD> ");
// Navigate to the previous page
If (currentpageindex> 0)
Writer. Write (string. Format (link_prev, (currentpageindex) + query ));
Else
...{
Writer. Write ("<TD> & nbsp; Previous Page & nbsp; </TD> ");
}

// Obtain page number navigation
For (INT I = 0; I <= 9 & I <= pagecount & (currentpageindex-currentpageindex % 10 + I) <= pagecount; I ++)
...{
If (currentpageindex-currentpageindex % 10 + I = currentpageindex)
...{
Writer. Write ("<TD> & nbsp;" + (currentpageindex + 1) + "& nbsp; </TD> ");
}
Else
...{
Writer. Write (string. Format (link_display, (currentpageindex-currentpageindex % 10 + I + 1) + query, (currentpageindex-currentpageindex % 10 + I + 1 )));

}
}

// Navigate to the next page
If (currentpageindex <pagecount)
Writer. Write (string. Format (link_more, (currentpageindex + 2) + query ));
Else
...{
Writer. Write ("<TD> & nbsp; next page & nbsp; </TD> ");
}

// Close the table
Writer. Write (html4 );
}

Override protected void ondatabinding (eventargs e )...{

// Work out which items we want to render to the page
Int start = currentpageindex * pagesize;
Int size = math. Min (pagesize, itemcount-Start );

Ilist page = new arraylist ();

// Add the relevant items from the datasource
For (INT I = 0; I <size; I ++)
Page. Add (datasource [start + I]);

// Set the base objects datasource
Base. datasource = page;
Base. ondatabinding (E );

}

Public event datagridpagechangedeventhandler pageindexchanged;

Virtual protected void onpageindexchanged (datagridpagechangedeventargs e )...{
If (pageindexchanged! = NULL)
Pageindexchanged (this, e );
}
}
}
3. Create a new class library project, save the above Code as customlist. CS, and generate a new user-defined component on the toolbar.

4. Call it

1. Create a website and drag a customlist component on the new page.

2. Set foreground attributes for the customlist component

<%... @ Page Language = "C #" autoeventwireup = "true" codefile = "default. aspx. cs" inherits = "_ default" %>

<%... @ Register Assembly = "customlist" namespace = "customlist" tagprefix = "PC3" %>

<! Doctype HTML public "-// W3C // dtd xhtml 1.0 transitional // en" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<HTML xmlns = "http://www.w3.org/1999/xhtml">
<Head runat = "server">
<Title> No title page </title>
</Head>
<Body>
<Form ID = "form1" runat = "server">

<PC3: customlist id = "productslist" runat = "server" emptytext = "no products found. "onpageindexchanged =" pagechanged "pagesize =" 12 "repeatcolumns =" 1 "cellpadding =" 4 "width =" 400px "currentpageindex =" 1 "forecolor =" #333333 ">
<Itemtemplate>
<Table cellpadding = "0" cellspacing = "0">
<Tr>
<TD valign = "TOP" width = "400px" align = "center"> <%... # eval ("DC") %> </TD>


</Tr>
</Table>
</Itemtemplate>
<Footerstyle backcolor = "#990000" font-bold = "true" forecolor = "white"/>
<Selecteditemstyle backcolor = "# ffcc66" font-bold = "true" forecolor = "Navy"/>
<Alternatingitemstyle backcolor = "white"/>
<Itemstyle backcolor = "# fffbd6" forecolor = "#333333"/>
<Headerstyle backcolor = "#990000" font-bold = "true" forecolor = "white"/>
</PC3: customlist>
</Form>
</Body>
</Html>
3. Bind the background page program

Using system;
Using system. Data;
Using system. configuration;
Using system. Web;
Using system. Web. Security;
Using system. Web. UI;
Using system. Web. UI. webcontrols;
Using system. Web. UI. webcontrols. webparts;
Using system. Web. UI. htmlcontrols;
Using system. collections;
Public partial class _ default: system. Web. UI. Page
...{
Protected void page_load (Object sender, eventargs E)
...{
}
Protected void pagechanged (Object sender, datagridpagechangedeventargs E)
...{
// Reset Index
Productslist. currentpageindex = E. newpageindex;

// Construct the table to be displayed as a data source
Datatable dt = new datatable ("tabe1 ");
Datacolumn Dc = new datacolumn ("DC ");
DT. Columns. Add (DC); // construct a column
For (INT I = 1; I <= 100; I ++) // cyclically add data
...{
Datarow DR = DT. newrow ();
Dr ["DC"] = "data" + I;
DT. Rows. Add (DR );

}

// Bind data
Productslist. datasource = DT. defaultview;
Productslist. databind ();

}
}

This article from the csdn blog, reproduced please indicate the source: http://blog.csdn.net/chenmintong/archive/2007/06/27/1669427.aspx

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.