-Teach you how to write the Ajax-driven DataGrid Control (2)

Source: Internet
Author: User
Tags listsource
From http://www.cnblogs.com/wfyfngu/archive/2007/10/09/917789.html

Ii. Guiding Ideology
Everything requires an idea as a guide, and our controls are no exception.
1. Name of the control (let's call it ajaxgrid, which will be used all the time later)
2. For control-oriented users (developers), we require code to be as concise as possible and rich APIs (including JavaScript)
3. for end users, we require rapid response, exquisite UI, and usage (localization)
In the above aspects, we enter the subject and start our coding journey.

3. Write control code
The user control of Asp.net is very exciting. It is more practical than code reuse promoted by any oo, because even if any person is Oo, it is still possible for him to rewrite the code in the next project, and the control, whether it is a beginner or veteran, reuse is inevitable. You don't need to master too many server control development methods, because our control needs to be implemented and won't use more advanced controls, for example, template control, callback, data binding, and status management, all we need is to override the render method.
Why rewrite the render method, because we need to output our own HTML on the page, and the original appearance of our control is an HTML table. However, there is another problem. We need to flip pages and filter data, which requires that our table be re-painted. Without a doubt, the re-painted data certainly comes from the server. Refresh the page is a common method, but don't forget, we are doing Ajax tables Oh ^_^. Maybe you will think of xmlrequest, which is exactly what we can think of very easily. You don't need to refresh the problem to solve. But what if the server does not retain the data source of the table? When we request it again later to generate a table that meets the requirements, it tells you that it has lost the data source, which won't work. In fact, the problem is also very simple. We let it keep the data source for future paging and data filtering, until we are sure we will not use this data source (ajaxgrid destruction), and then "discard" it ".
Next we will gradually implement the above points.
1. Specify a data source for the control and save it until it is no longer needed.
We chose system. Data. able as our final data source, because it is easy to sort, flip, and filter data. However, the control user (developer) does not want to generate a able every time to use our control. What should I do? Since others don't want to do it, we can do it ourselves. We found that the ilistsource or ienumerable interface is implemented for data sources that can be bound to server controls. That is to say, we only need to check whether one of the two interfaces is implemented for the data source specified by the control, (if not? Do you want to ask this question? If you want to give him an exception and see if he doesn't dare to do so next time?) and then convert them into able. The Code is as follows: using system;
Using system. Data;
Using system. collections;
Using system. componentmodel;

Namespace wfyfngu. Web. UI
{
/** // <Summary>
/// Summary of performancehelper.
/// </Summary>
Internal class performancehelper
{


Private Static ienumerable resolvedatasource (Object datasource, string datamember)
{
// If it is ilistsource
If (datasource is ilistsource)
{
Ilistsource listsource = (ilistsource) datasource;
Ilist list = listsource. getlist ();
//
If (listsource. containslistcollection)
{
Itypedlist typedlist = (itypedlist) List;
Propertydescriptorcollection collection = typedlist. getitemproperties (New propertydescriptor [0]);
If (collection. Count = 0)
Throw new exception ("listsource without datamembers ");

Propertydescriptor descriptor = NULL;
If (datamember = NULL | datamember = string. Empty)
{
Descriptor = Collection [0];
}
Else
{
Descriptor = collection. Find (datamember, true );
}
If (descriptor = NULL)
Throw new exception ("listsource missing datamember ");

Object listitem = list [0];
Object member = descriptor. getvalue (listitem );

If (member = NULL |! (Member is ienumerable ))
Throw new exception ("listsource missing datamember ");

Return (ienumerable) member;
}
Else
{
Return (ienumerable) List;
}
}
If (datasource is ienumerable)
{
Return (ienumerable) datasource;
}

Throw new exception ("None ilistsource or ienumerable .");
}

/** // <Summary>
/// Convert the data sources that implement ilistsource and ienumerable into datatable
/// </Summary>
/// <Param name = "datasource"> </param>
/// <Returns> </returns>
Internal static datatable conventtodatatable (Object datasource)
{
If (datasource = NULL)
Return new datatable ();
Ienumerable Ie = resolvedatasource (datasource, null );
Datatable table = new datatable ();
Int colindex = 0;
Arraylist rows = new arraylist ();
Foreach (Object item in IE)
{

If (item is datarowview)
{
Table = (datarowview) item). dataview. Table;
For (INT I = 0; I <Table. Columns. Count; I ++)
Table. Columns [I]. columnname = "F" + I. tostring ();
Return table;
}
//
Table. Columns. Add ("F" + (colindex ++). tostring ());
Rows. Add (item );
}
Object [] O = new object [rows. Count];
For (INT I = 0; I <O. length; I ++)
O [I] = rows [I];
Table. Rows. Add (O );
Return table;
}

}
}

>>To be continued! Control source code and a brief introduction, please log on to the http://www.codeassistant.net/wfygrid/ <

Related Article

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.