Design and use of basic pages in Asp.net

Source: Internet
Author: User

In the development process of the Asp.net business system, in order to ensure the consistency of the page style and reduce duplicationCodeWe need to introduce the concept of base class pages, that is, to define a base class page so that all pages can inherit this base class, add common attributes and methods to the base class page.

In actual use, you can define multiple base class pages according to the functional pages, such:
Class formbase
Class bizformbase: formbase
Class viewformbase: bizformbase
Class editformbase: bizformbase
Class queryformbase: formbase

Formbase: the root in the base class page, providing services unrelated to the business, such as URL rewriting and logs.
Bizformbase: add business-related attributes, such as the current business object ID of the page.
Viewformbase,: editformbase: allows you to view and edit data.
Queryformbase: encapsulate generic query pages.

From the object-oriented perspective, the design of the base class page and the general base class and the inheritance class are actually not very different. You must write common attribute methods in the base class, and the inheritance class can be rewritten or response through virtual functions, events, and other methods. The difference is that the design process of the base page is restricted by the environment. In the winform environment, we can pre-define the common elements of the form, such as toolbar, default table, and datasource control. However, webform under Asp.net cannot implement interface-level inheritance, and State management and other requirements are added.

To help you understand, we analyze the design process of querying the base page:

A simple query page consists of three parts: Multiple query condition text boxes, query buttons, and tables. At the same time, the query page is associated with a data access component. When querying a vertex, the query condition is converted into a where statement and submitted to the data access component.

Querypeopleform
Onquerybuttonclick ()
{
String peoplename = txtpeoplename. text;
String leleage = txtpeopleage. text;
 
String SQL;
SQL = string. Format ("name like '% {0} %' and age = {1}, peoplename, peopleage );
 
Lelemanager manager = new peoplemanager ();
This. gridmain. datasource = manager. getdatatable (SQL );
This. gridmain. databind ();
}

When you click the query button, we do the following:
1. Obtain query Conditions
2. Submit the query
3. Bind the query result to the table

The submitted query and binding here are the same on different query pages, so we first put in the base class page, and provide such a method: void queryandbind (imanager manager, string SQL); here you need to define the imanager interface so that all managers can implement this interface, so that the base class page does not need to know the specific manager, as long as you call the imanager. the getdatatable method, and then bind the table to it.

Code after using the base class page:

Querypeopleform: queryformbase
Onquerybuttonclick ()
{
String peoplename = txtpeoplename. text;
String leleage = txtpeopleage. text;
SQL = string. Format ("name like '% {0} %' and age = {1}, peoplename, peopleage );
Queryandbind (New peoplemanager (), SQL );
}

The code here is missing, but there is still a problem. When the query condition changes, the work of each query statement is boring and error-prone, So we add a query class, to simplify the operation here:

Public Enum queryoperator
{
// Equals to comparison.
Equal = 0,
// Not equal
Notequal = 1,
// Like comparison
Like = 6
}

Class Query
{
Void add (string fieldname, string value, queryoperator operator );
String getsql ();
}

Querypeopleform: queryformbase
Onquerybuttonclick ()
{
Query query = new query ();
Query. Add ("name", txtpeoplename. Text, queryoperator. Like );
Query. Add ("Age", txtpeopleage. Text, queryoperator. Equal );
Queryandbind (New lelemanager (), query. getsql ());
}

Put SQL spelling in the query class. The caller only needs to declare the query field, corresponding value, and comparison type.

In this step, our base class page is very useful, but there is still a small problem, that is, what we mentioned earlier, that is, interface-level inheritance cannot be implemented in webform, the queryandbind method of the base class page will not be able to know which table the query result is bound to. In this case, we will declare the defaultgrid attribute on the base class page, let the inheritance page inform the current table control.

Modified code:
Querypeopleform: queryformbase
Onquerybuttonclick ()
{
Initcontrols (gridmain );
Query query = new query ();
Query. Add ("name", txtpeoplename. Text, queryoperator. Like );
Query. Add ("Age", txtpeopleage. Text, queryoperator. Equal );
Queryandbind (New lelemanager (), query. getsql ());
}

At this point, the basic page functions are complete, but it is still not enough. If we want to change the query Button clicking behavior in the future, for example, if the query result is blank, a dialog box prompt is displayed, at this time, we still need to modify the Page code everywhere. This is not what we want, so we put the onclick operation of querybutton in the base class page for execution, on the Inheritance page, you only need to initialize the data access component and set query conditions.

Querypeopleform: queryformbase
Void initialize ()
{
// Specify the manager corresponding to the page
Manager = new peoplemanager ();
// Bind controls
Initcontrols (gridmain, btnquery );
}

Void getqueryinfo (query)
{
// Obtain query Conditions
Query. Add ("name", txtpeoplename. Text, queryoperator. Like );
Query. Add ("Age", txtpeopleage. Text, queryoperator. Equal );
}

Queryformbase:

Private imanager manager = NULL;
Public imanager Manager
{
Get {return manager ;}
Set {manager = value ;}
}

Void initcontrols (gridview grid, button querybutton)
{
This. defaultgrid = grid;
This. querybutton = querybutton;
Querybutton. Click + = new eventhandler (querybutton_click );
}

Void querybutton_click (Object sender, eventargs E)
{
Query query = new query ();
Getqueryinfo (query );
Queryandbind (Manager, query. getsql ());
}

The total code can be downloaded from the attachment. You can add breakpoints to check the base class and inherit the code execution sequence of the class page.
It should be said that the design of the base class is relatively complex, but the advantage is that the Code that inherits pages has become clear and there is no redundant code.

The design of the base page is actually skillful, and the following are summarized:
1. First, write the Page code in the most direct way.
2. extract common methods and add auxiliary classes
3. Extract the event processing process to the base class page
4. design methods and events for inheriting page overloading in the base class page

In terms of design, it is very intuitive to use base-class pages to unify operations and simplify Page code. The disadvantage is that as the project evolves, the base-class pages will become larger and complete, it is not easy to be reused by new projects. In this case, we can consider putting some of the functions into user controls and custom components to reduce coupling and improve reusability.

Code example:/files/Jiyang/queryformdemo.rar

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.