Asp.net inherits from the page to implement unified page verification and error handling

Source: Internet
Author: User

CopyCode The Code is as follows: isadmin ();

Because the master page was not used at the time, the master page cannot be used to determine the permissions in a unified manner. At that time, I was limited to my own level, the method of inheriting from the page class is not used to process all the things that need to be handled during page loading. According to some code records of "Li Tianping (dynamic soft)", I hope you will learn how to use inheritance!
Let's look at the next simple pagebase inherited from the page:Copy codeThe Code is as follows: using system;
Using system. Data;
Using system. configuration;
Using system. Web;
Using system. Web. Security;
Using system. Web. UI;
Using system. Web. UI. htmlcontrols;
Using system. Web. UI. webcontrols;
Using system. Web. UI. webcontrols. webparts;

/// <Summary>
/// First write by Li Tianping
/// Up by ahuinan 2009-4-18
/// </Summary>
Public class pagebase: system. Web. UI. Page
{
Public pagebase ()
{
//
// Todo: add the constructor logic here
//
}

Protected override void oninit (eventargs E)
{
Base. oninit (E );
This. Load + = new system. eventhandler (pagebase_load );
This. Error + = new system. eventhandler (pagebase_error );

}

// Handle errors
Protected void pagebase_error (Object sender, system. eventargs E)
{
String errmsg = string. empty;
Exception currenterror = httpcontext. Current. server. getlasterror ();
Errmsg + = ""This information has been recorded by the system. Please try again later or contact the administrator. <Br/> "+
"Error address:" + request. url. tostring () + "<br/>" +
"Error message:" + currenterror. Message. tostring () + "<HR/>" +
"<B> stack trace: </B> <br/>" + currenterror. tostring ();
Httpcontext. Current. response. Write (errmsg );
Server. clearerror ();
}

Private void pagebase_load (Object sender, eventargs E)
{
If (! Page. ispostback)
{
If (httpcontext. Current. session ["username"]! = NULL)
{
Httpcontext. Current. response. Write ("Search for sosuo8.com logon test ");
}
Else
{
Httpcontext. Current. response. Write ("You are not a kinnan, do not log on ");
}
}
}
}

When using:Copy codeThe Code is as follows: public partial class _ default: pagebase
{

Protected void page_load (Object sender, eventargs E)
{
Int id = int. parse (request. querystring ["ID"]);
Response. Write ("ID:" + id. tostring ());
}
}

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.