Data binding process
The DATABIND method is all ASP.NET controls, for the data binding control, it will trigger the refresh of the user interface to reflect new data. The SQLPAGER control uses this method to start the data search operation using this method according to the value of the selectcommand and the connectionstring property. It goes without saying that if any of these properties is empty, the process will terminate. Similarly, if the partner control does not exist, the data binding process will be canceled. To find partner controls, Database uses the FindControl function in the Page class. It can be seen that the partner control must be a direct sub-control of the main form.
Controls that make paging display cannot be arbitrary ASP.NET server controls. It must be a list control or basic data list control. More generally, the partner control must have a DataSource property and implement the DataBind method. Controls that may be paging actually only need to meet these requirements. All inherited ListControl or baseDataList's controls in Microsoft? .NET Framework meet the first requirements; all web controls meet Database requirements through the design. Using the current implementation method, you cannot use the SQLPAGER control to page the Repeater. The REPEATER is different from the partner control DataList and DataGrid, and does not inherit the BaseDataList, nor does it provide the function of the list control. The following table lists the controls that can be paid using SQLPAGER.
Table 2: Data Binding Controls that can be paid by SQLPAger controls
Control description
CheckBoxList is derived from ListControl and is displayed as a list of checkboxes.
DropDownList is derived from ListControl and is displayed as a string drop-down list.
Listbox is derived from ListControl and is displayed as a string to scroll through the list.
RadiobuttonList is derived from ListControl and is displayed as a radio button list.
DataList is derived from BaseDataList, which is displayed as a list of templated data items.
DataGrid is derived from BaseDataList, displayed as a table grid of a data project. DataGrid is the only ASP.NET control for a built-in powerful paging engine.
The following code illustrates the data binding process implemented by the SQLPAger control.
Public override void data
{
// Start data binding event
Base.DATABINDING ();
// must be recreated after data binding
ChildControlscreated = false;
/ / Make sure the control exists and lists the list control
_ControlTopaginate = page.FindControl (controltopaginate);
IF (_ControlTopaginate == null)
Return;
IF (! (_ controltopaginate is basedater) || _controltopaginate is listcontrol))
Return;
/ / Make sure there is enough connection information and specify a query
IF (Connectionstring == "" || selectcommand == "")
Return;
// retrieve data
IF (PagingMode == PagingMode.cached)
FetchaLdata ();
Else
FetchPageData ();
// Bind the data to the partner control
BasedataList BaseDataListControl = NULL;
ListControl ListControl = NULL; if (_Controltopaginate IS BasedatAlist)
{
BasedataListControl = (baseDataList) _Controltopaginate;
BaseDataListControl.DataSource = _DataSource
BasedataListControl.DATABIND ();
Return;
}
IF (_Controltopaginate IS ListControl)
{
ListControl = (listcontrol) _Controltopaginate;
ListControl.Items.clear ();
ListControl.DataSource = _DataSource;
ListControl.DATABIND ();
Return;
}
}
Different acquisition routines are called according to the value of the PagingMode property. In any case, the result set binds to the instance of the PagedDataSource class. This class provides some features used to page data. In particular, when the entire data set is stored in the cache, the class will automatically retrieve the record of the current page and return to the Boolean value to provide information about the first page and the last page. The internal structure of this class will be described later. In the above list, the PageDataSource object of the help program is represented by the _DataSource variable.
The SQLPAger control then calculates the type of the partner control and binds the contents of the PageDataSource object to the DataSource property of the partner control.
Sometimes the above DATABIND method also re-sets the ChildControlScreated property to false. So why do you do this?
When the page containing the paging program is returned, all controls are recreated; paging programs are no exception. Normally, all controls and their sub-controls are created before preparing to display the page. At a moment before receiving an onprender notification, the protected ensurechildControls method will be called so that each control can generate its own control tree. After this event, the data binding process is complete, and the new data is stored in the cache.
However, when the page returns due to a constituent control of the paging program (ie, the user clicks to change the page), the control tree of the sub-page program will be generated, and the display phase is not reached. In particular, when processing related server-end events, the control tree must be generated, so that the control tree is generated before the data binding begins. Difficulties are that the data binding will modify the page index, which must be reflected in the user interface. If you do not take some countermeasures, when the user switches to another page, the page index in the paging program will not be refreshed.
There are various ways to solve this problem, but it is important to figure out the problem and its real reasons. You can avoid generating control trees and generate all outputs in the Render method. Alternatively, you can also modify the part of the affected data binding changes in the tree. This article selects a third method, which requires less code, and it is possible to solve the problem regardless of which portion of the user interface is subject to data binding changes. You can invalidate the trees of previously created controls by setting the ChildControlScreated property to False. This will recreate the control tree before display.
Pagination engine
The SQLPAger control supports two ways to retrieve data - cache and non-cache. If the former is used, select commands to execute, and the entire result set will be bound to the data source object that is paid inside. The PageDDataSource object will automatically return a record that is suitable for a specific page. The PagedDataSource class is also a system component that is running behind the DataGrid default paging mechanism. Retrieve all records but only a few records suitable for the page, which is usually not a wise approach. Due to the stateless characteristics of the web application, in fact, a large number of queries may be run each time the page is requested. To make the operation valid, adopting a cache data retrieval method must rely on a cache object, ASP.NET's Cache object is a good candidate object. The use of cache technology speeds up the running speed of the application, but the data snapshot provided cannot reflect the latest changes. In addition, it needs to use more memory on the web server. And ridiculous is that if a large amount of data is cached by a session, it may even cause a big problem. The Cache container is globally for the application; if the data is stored in it, you need to generate a specific item name with a session.
The top of the Cache object is fully supported expiration strategy. In other words, the data stored in the cache can be automatically released after a period of time. The following code illustrates a private method used to get the data and store it in the cache in the SQLPAger class.
Private void fetchaalldata ()
{
// Find data in ASP.NET Cache
DataTable data;
Data = (DATATABLE) Page.cache [cachekeyname];
IF (data == null)
{
// Modify SELECTCOMMAND using Order-by information
AdjustSelectCommand (TRUE);
// Go to the database if the data expires or never been acquired
SqlDataAdapter Adapter = New SqldataAdapter (SELECTCOMMAND, CONNECTION);
Data = new datatable ();
Adapter.Fill (data);
Page.cache.insert (CacheKeyname, Data, NULL,
DateTime.now.addseconds (Cacheduration),
System.Web.caching.cache.noslidingExpiration;
}
// Configuring the data source components of the paging
IF (_DataSource == null)
_datasource = new pagedDataSource ();
_DataSource.DataSource = data.defaultview;
_DataSource.Allowpaging = true;
_DataSource.pageSize = itemsperpage;
Totalpages = _datasource.pagecount;
/ / Make sure the page index is valid
ValidatePageIndex ();
IF (currentpageIndex == -1)
{
_DataSource = null;
Return;
}
// Select the page you want to view
_DataSource.currentPageIndex = CURRENTPAGEINDEX;
}
The control and request cache project name are unique. It includes a page of the URL and the ID of the control. Data is bind to the cache within the specified time (in seconds). To expire, you must use the cache.insert method. The following simpler code adds the item to the cache, but does not include any expiration strategy.
Page.cache [cachekeyname] = data; PagedDataSource Object Gets data to be paid by its DataSource property. It is worth noting that the DataSource attribute of the PagedDataSource class accepts only the IEnumerable object. DataTable does not satisfy this request, this is why the defaultview property is taken.
The SelectCommand property determines the query running on the SQL Server database. This string is preferably in the form of select-from-where. The ORDER BY clause is not supported, and if the clause is specified, it will be deleted. This is what the AdjustSelectCommand method is. Use the sortfield property to specify any sorting information. The AdjustSelectCommand method itself will add a correct ORDER BY clause according to the value of sortfield. Does this do this?
When the paging program operates in a noncached mode, the original query will be modified to ensure that the current page record is retrieved. The actual query text performed on SQL Server will take the following form.
SELECT * FROM
(SELECT TOP ITEMSPAGE * FROM
(SELECT TOP ITEMSPAGE * CURRENTPAGEINDEX * FROM
(SelectCommand) AS T0
Order by Sortfield ASC) AS T1
Order by Sortfield DESC) AS T2
Order by Sortfield
The query makes up for the defect of the ROWNUM clause in SQL Server 2000, and reorders the record so that only the "Nth" block of the x item is returned after proper sorting. You can specify a base query, and the paging program will be broken down into multiple smaller pages. Only records suitable for a page are returned. As you can see, in addition to the query command, the above query needs to process the sort field. That's why add the sortfield property added. The only drawback of this code is that the default is sorted in ascending. This code is really perfect by allowing ASC / DESC keywords.
Private void fetchpagedata ()
{
// Need to pass the validated page index to get the data.
// The actual page number is also required to verify the page index.
AdjustSelectCommand (False);
VirtualRecordcount Count Count Countyfo = CalculatevirtualRecordcount ();
Totalpages = countInfo.pagecount;
// Verify the page number (make sure that CurrentPageIndex is valid or "-1")
ValidatePageIndex ();
IF (currentpageIndex == -1)
Return;
// Prepare and run the command
SQLCOMMAND CMD = prepareCommand (countinfo);
IF (cmd == null)
Return;
SqlDataAdapter Adapter = New SqldataAdapter (CMD);
DataTable Data = New DataTable ();
Adapter.Fill (data);
// Configuring the data source components of the paging
IF (_DataSource == null)
_datasource = new pagedDataSource ();
_DataSource.AllowCustomPaging = true;
_DataSource.Allowpaging = true;
_DataSource.currentPageIndex = 0; _datasource.pageSize = itemsperpage;
_DataSource.virtualcount = countinfo.recordcount;
_DataSource.DataSource = data.defaultview;
}
In Noncached mode, the PageDDataSource object does not provide the entire data source, so the total number of pages to be paneled cannot be calculated. Thereby, the AllowCustomPaging property must be tagged and the actual number of records in the data source is provided. Typically, the actual number is to retrieve the Select Count (*) query. This model is almost the same as DataGrid's custom paging. In addition, the current page index selected in the PagedDataSource object is usually 0, because one page record has been stored.
The implementation method of the SQLPAger control is introduced here. Let's introduce it to its method.
Use SQLPAGER controls
Assume that there is a sample page containing the ListBox control. To use a paging program, make sure the .aspx page registers the assembly of the control.
<% @ Register tagprefix = "expo" namespace = "devcenter" assembly = "sqlpager"%>
The tag of the control depends on the properties of the actual setting. The following tags are an rational example:
DataTextField = "CompanyName" /> Controltopaginate = "ListBox1" SelectCommand = "Select Customerid, CompanyName from Customers" Connectionstring = "Server = localhost; database = northwind; uid = ..." SortKeyfield = "CompanyName" /> In addition to paged programs, the page also contains a list box and a button. The list box will display the contents of each page; the button is only used for the first time fill the list box. This button has a clicking event handler, defined as follows. Private void loadingfirst1_click (Object sender, eventargs e) { SQLPAGER1.CURRENTPAGEINDEX = 0; Sqlpager1.databind (); } Figure 2 shows the page in the operation. Figure 2: SQLPAger control works with the ListBox control. Use the DataList control to generate a more interesting example. The goal is to use the paging program to browse the personal record of each Northwind staff. This DataList is shown in the following list. Font-names = "Verdana" font-size = "8pt">