Sunday, June 24, 2012

Inheritence in C#, overriding base class

In the following code I am overriding the MasterPage class's onLoad event. What I am doing is, I check whether session variables have been set i.e. if the user has actually logged in into the system. I have created the baseclass titled baseClass.
public class baseClass : System.Web.UI.MasterPage
{
 public baseClass()
 {
  //
  // TODO: Add constructor logic here
  //
 }
    protected override void OnLoad(EventArgs e)
    {
        try
        {
            if (Session["usrPrivilege"].ToString().Equals("ADMIN"))
            {
                base.OnLoad(e);
            }
            else

                Response.Redirect("~/Default.aspx");
        }
        catch (NullReferenceException ex)
        {
            Response.Redirect("~/Default.aspx");
        }
        catch
        {
            base.OnLoad(e);
        }
        
    }
}
Now in the masterpage I wrote the following code, in this way I am sparing the onLoad eventhandler of my masterpage from checking session related information
public partial class Pages_ADMIN_MasterPage :baseClass
{
    protected void Page_Load(object sender, EventArgs e)
    {
        try
        {
          
            if (IsPostBack == false)
            {
                //My code goes here
            }
        }
         
        catch (System.Exception ex)
        {
                 //My code for exception handling
        }
    }
}
For further reading you can click here

No comments: