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.
Raul77Asked:
Who is Participating?
 
Anurag ThakurConnect With a Mentor Technical ManagerCommented:
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
 
Anurag ThakurTechnical ManagerCommented:
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
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.