Use datapager in silverlight3 to implement server-side Paging

Source: Internet
Author: User
[Transfer]

The blog describes how to use datapager to implement client paging:
Http://www.cnblogs.com/daizhj/archive/2009/08/07/1529331.html
Generally, this client paging mode is rarely used in projects unless the data volume is small (not counted by PAGE ).
It is not wise to transmit a large amount of data to the client at a time. We generally pass in search condition parameters (including filter conditions and paging condition parameters) from the client ), the server then finds the list of records that meet the search criteria from the database and transfers them to the client. The client is bound to the DataGrid Control.
Here, we use the "Silverlight-enabled WCF Service" to communicate with the client (Silverlight) program. The data access adopts the ado.net Entity Framework and the solution structure.

A database for testing is created, which contains two tables:

This instance not only demonstrates the use of datapager's server page, but also implements the dynamic search function using Entity SQL:

Many paging controls have been written, but unlike datapager, pagedcollectionview is required. Generally, the total number of records and page size are required, we added an extension method for datapager to bind the total number of records and the page size:

Code
Public static class datapageextension
{
Public static void bindsource (this datapager, int totalcount, int pagesize)
{
List <int> List = new list <int> (totalcount );
For (INT I = 0; I <totalcount; I ++) List. Add (I );
Pagedcollectionview PCV = new pagedcollectionview (list );
PCV. pagesize = pagesize;
Datapager. Source = PCV;
}
}

The paging method of the WCF server is as follows:

Code
[Operationcontract]
Public list <myemployee> getemployeelist (employeefilter filter, out int totalcount)
{
Using (testdbentities DB = new testdbentities ())
{
Int rowscount = 0;
Stringbuilder sbsql = new stringbuilder ("true ");
If (filter. deptid! = New GUID ())
Sbsql. append (string. Format ("and it. Departments. Dimension mentid = guid '{0}'", filter. deptid ));
Sbsql. append (string. Format ("and it. empolyeename like '% {0} %'", filter. empname ));

VaR query = from EMP in db. Employees. Where (sbsql. tostring ())
Select New myemployee
{
Id = EMP. employeeid,
Name = EMP. empolyeename,
Sex = EMP. employeesex? "Male": "female ",
Age = EMP. employeeage,
Address = EMP. employeeaddress,
Deptname = EMP. Parameters. departmentname
};
If (filter. pageindex <= 0)
Rowscount = query. Count ();
Totalcount = rowscount;
Query = query. orderby (t => T. Name). Skip (filter. pageindex * filter. pagesize). Take (filter. pagesize );
Return query. tolist ();
}
}

The above code implements the dynamic search and paging functions of Entity SQL. We can see that the total number of records is calculated only when pageindex is 0, which improves the efficiency of method execution; the input and output parameters of the method are encapsulated in the object class. We recommend that you do the same in the actual project, especially when using dependency injection.
After the service is referenced in the client (Silverlight project), the CS code on the page is as follows:

Code
Using system;
Using system. Collections. Generic;
Using system. LINQ;
Using system. net;
Using system. windows;
Using system. Windows. controls;
Using system. Windows. documents;
Using system. Windows. input;
Using system. Windows. Media;
Using system. Windows. Media. animation;
Using system. Windows. shapes;
Using system. Windows. Data;
Using datapagertest. employeeservicereference;

Namespace datapagertest
{
Public partial class mainpage: usercontrol
{
Employeeserviceclient client = new employeeserviceclient ();
Employeefilter filter = new employeefilter ();
Public mainpage ()
{
Initializecomponent ();
}
Private void usercontrol_loaded (Object sender, routedeventargs E)
{
Dpemployee. pageindexchanged + = new eventhandler <eventargs> (dpemployee_pageindexchanged );
Cbdept. selectionchanged + = new selectionchangedeventhandler (cbdept_selectionchanged );
Bindcombox ();
}

Void cbdept_selectionchanged (Object sender, selectionchangedeventargs E)
{
Bindgrid (0 );
}
Void dpemployee_pageindexchanged (Object sender, eventargs E)
{
Bindgrid (dpemployee. pageindex );
}
Private void btnquery_click (Object sender, routedeventargs E)
{
Bindgrid (0 );
}
Private void bindgrid (INT pageindex)
{
Parameters dept = cbdept. selecteditem as parameters;
Filter. deptid = Dept. inclumentid;
Filter. empname = tbempname. Text. Trim ();
Filter. pageindex = pageindex;
Filter. pagesize = 9;
Client. getemployeelistcompleted + = new eventhandler <getemployeelistcompletedeventargs> (client_getemployeelistcompleted );
Client. getemployeelistasync (filter );
}
Void client_getemployeelistcompleted (Object sender, getemployeelistcompletedeventargs E)
{
Dgemployee. itemssource = E. result;
If (filter. pageindex <= 0)
Dpemployee. bindsource (E. totalcount, filter. pagesize );
}
Void bindcombox ()
{
Client. getdepartmentlistcompleted + = new eventhandler <getdepartmentlistcompletedeventargs> (client_getdepartmentlistcompleted );
Client. getdepartmentlistasync ();
}

Void client_getdepartmentlistcompleted (Object sender, getdepartmentlistcompletedeventargs E)
{
Cbdept. itemssource = E. result;
Cbdept. displaymemberpath = "departmentname ";
Cbdept. selectedindex = 0;
Bindgrid (0 );
}



}
}

When pageindex is equal to 0, call the bindsource Extension Method to bind the total number of records and page size. There is also a data binding method for ComboBox. I don't know what you think about this paging method. Please click it.

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.