Add a base class to the page

Source: Internet
Author: User

Let's talk about logon. If there are many pages that need to be viewed only after logon, follow the common practice and check whether there is a logon on the page that requires logon, such as viewing cookies, session and so on. If you like it, but you have to write this code on every page, isn't it a little troublesome?

Then we will introduce the base class idea. Every page inherits from this base class. It is easy and quick to write a judgment in the base class.

First, add an app_code folder and add a class under the folder. The name is casual. For convenience of memory, my name is basepage. cs. The content is:

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;

///   <Summary>
/// Basepage Summary
///   </Summary>
Public   Class Basepage: Page
{
Public Basepage ()
{
//
// Todo: add the constructor logic here
//
}

///   <Summary>
/// Retrieve session information
///   </Summary>
///   <Returns> </returns>
Public   String Getuserinfo ()
{
String Userinfo =   "" ;
If (Session [ " Userinfo " ] ! =   Null )
{
Userinfo = Session [ " Userinfo " ]. Tostring ();
}
Return Userinfo;
}

///   <Summary>
/// Page redirection
///   </Summary>
Private   Void Pageredirect ()
{
Response. Redirect ( " Login. aspx " );
}

# Region Pageinit
///   <Summary>
/// Reload the preinit event to control theme
///   </Summary>
///   <Param name = "E"> </param>
///   <Remarks>
///   </Remarks>
Protected   Override   Void Onpreinit (eventargs E)
{
Base . Onpreinit (E );
System. Web. profile. profilebase profile;
Profile = Httpcontext. Current. profile;
This . Theme =   " Default " ;
}

///   <Summary>
/// Get userinfo when pageload is used to determine whether the session is invalid. If the session fails, return to the logon page.
///   </Summary>
///   <Param name = "E"> </param>
Protected   Override   Void Onload (eventargs E)
{
If (Getuserinfo () =   "" )
{
Pageredirect ();
}
Base . Onload (E );
}
# Endregion
}

 

In this base class, we define two very simple functions: Implement skin control and determine whether to log on.

Create two pages in the project: one login. aspx and the other default. aspx.

Login. aspx is very simple. You just need to write it to the session.

The content of default. aspx is:

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;

PublicPartialClass_ Default: basepage
{
Protected VoidPage_load (ObjectSender, eventargs E)
{
Response. Write (session ["Userinfo"]);
}
}

The basepage is inherited, so if default. aspx is opened directly, it will be redirected to login. aspx. Is it much easier?

This is just an idea. You can write a lot of content in the base class, but it also has advantages and disadvantages.

Below are professionalArticleDescription in:

By default, ASP. NET ApplicationsProgramAll the Wen form pages in are inherited from system. Web. UI. Page. This class is implemented as a necessary function of httphandler. The ASP. NET Runtime Library will call this class when detecting a request to call a page. In short, httphandler is the terminal for processing requests. The page class is a specific implementation method used by httphandler to process and display web forms. Chapter 2 illustrates how to execute httphandler to complete other tasks.

It is always the primary task to find ways to reduce redundancy and flexibly build an Architecture Pattern for building an application. One mode that can be used is to define a page base class so that all pages can inherit this base class, rather than system. Web. UI. Page. In this way, even if you didn't use it at first, you can create a foundation to add properties and functions visible to all pages. This new base class inherits from system. Web. UI. Page, so that you do not have to repeat all the functions in the new base class of the page. By doing so, the new page base class will be built on the basis of the system. Web. UI. Page class function. In addition, this will allow the new base class to access all protected attributes, methods, and events of the system class.

Based on the scale and complexity of the created application, you can consider implementing multi-layer basic pages. For example, the underlying basic page is part of a practical class library, which is not dedicated to this application. However, inheriting this class library is specific to the currently created application. It provides useful attributes and methods in this application environment, such as activeproductid. Finally, if there is a set of pages that complete similar tasks, such as datainput or searchresults, You can inherit the basic page dedicated to the application to create a page related to this set of pages. These layers Provide extensions to reduce page redundancy.

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.