• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 780
  • Last Modified:

Override OnInit method and access in another class.

i have managed to override the OnInit method using :

public class PageBase : System.Web.UI.Page

{
    protected override void OnInit(
   EventArgs e)
    {
        if (Session["user"] == null)
        { Response.Redirect("Signup.aspx"); }
    }

    }

since its protected how can i call the method in other classes? (i tried making it public but no luck)

Thanks.
0
Raul77
Asked:
Raul77
  • 2
1 Solution
 
Anurag ThakurCommented:
if you inherit all you web pages from PageBase class i think that it will get called automatically

normally this kind of code is handelled on hte page load of the web page not on the OnInit event
0
 
Raul77Author Commented:
here is my code in my pagebase class :
public class PageBase : System.Web.UI.Page

{
    protected override void OnInit(
   EventArgs e)
    {
        if (Session["user"] == null)
        { Response.Redirect("Signup.aspx"); }
    }



    }

and this is what i am TRYING to do , but not working

public partial class Home : PageBase
{
    protected void Page_Load(object sender, EventArgs e)
    {
        PageBase test = new PageBase();
        test.OnInit();

ERROR:
Cannot access protected member 'System.Web.UI.Control.OnInit(System.EventArgs)' via a qualifier of type 'PageBase'; the qualifier must be of type 'Home' (or derived from it)
0
 
Anurag ThakurCommented:
i really dont think you need to do
 PageBase test = new PageBase();
        test.OnInit();
when you are inheriting from the PageBase class

when the class is initialized the OnInit will be called for the Home and will be called for PageBase class too
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now