asp.net inheritance from page to achieve unified page verification and error handling _ practical skills

Source: Internet
Author: User
Tags error handling httpcontext inheritance stack trace
Copy Code code as follows:

ISAdmin ();

Because I did not use the master page to do, so I can not be unified in the master page to judge permissions, and I was limited to their own level, and did not inherit from the page of this class method to unify the processing of some of the page load to deal with things. Now according to "Li Tianping (Dynamic Soft)" Some code records, also hope that we should learn to use inheritance Ah!
Look at a simple pagebase that inherits from page:
Copy Code code 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>
The I-write by Li Tianping
Up by Ahuinan 2009-4-18
</summary>
public class PageBase:System.Web.UI.Page
{
Public Pagebase ()
{
//
TODO: Add 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);

}

Error handling
protected void Pagebase_error (object sender, System.EventArgs e)
{
String errmsg = String. Empty;
Exception currenterror = HttpContext.Current.Server.GetLastError ();
ErrMsg + = ""This information has been logged by the system, please try again later or contact your administrator." <br/> "+
"Error Address:" + Request.Url.ToString () + "<br/>" +
"Error message:" + currentError.Message.ToString () + ""<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 bar sosuo8.com Landing test");
}
Else
{
HttpContext.Current.Response.Write ("You are not Ahanan, do not log in");
}
}
}
}

When in use:
Copy Code code as follows:

Public partial class _default:P agebase
{

protected void Page_ Load (object sender, EventArgs e)
{
int ID = Int. Parse (request.querystring["ID"]);
Response.Write ("ID:" +id.) ToString ());
}
}
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.