user3340627
user3340627

Reputation: 3143

Check on child page name from Parent Page

I have a PageContext class which inherits from Page. I got several children classes that inherit from PageContext. In PageContext, I got a property "Permission". I need to set "Permission" property based on the Child page that is open.

This is my PageContext

 public class PageContext: Page
    {
        public string permission
        {
            get
            {
                 switch ( the open child page)
                 {
                     case 'A':
                           return 'Read';
                     default:
                          return 'Read';

                 }
            }

        }
    }

Can I achieve this in the parent class or do i have to do it in each child page ?

Upvotes: 0

Views: 658

Answers (1)

Aria
Aria

Reputation: 3844

I have developed something same your code to check user access in Page_Load of each page as following:

So I have a base MemberWebPage like:

 public abstract class MemberWebPage : System.Web.UI.Page
{
    public virtual bool AutorizeUser()
    {
        return false;
    }
    protected override void OnInit(EventArgs e)
    {
        base.OnInit(e);
    }
}

And all pages that inherited should implement base methods (AutorizeUser) like:

  public partial class UsersManagement :MemberWebPage
{
    public override bool AutorizeUser()
    {
        EnteredUser up = (EnteredUser)Session["UserProfile"];
        if (up == null)
            Response.Redirect("../LogIn.aspx");
        return up.HasAccess(PrimitiveActivity.UserManagement);
    } 
    protected void Page_Load(object sender, EventArgs e)
    { 
        if (!AutorizeUser())
            Response.Redirect("../Login.aspx");
    }
}

My HasAccess method will check user access from DB !

But I am still not very sure about your code, anyway you can follow these steps (this can be a solution):

1- Define 'PageName' as virtual string in your PageContext it will return null

2- Each derived class will override PageName

3- In PageContext 'Permission' property will return according to the PageName

So we have:

 public class PageContext : Page
{ 
    public virtual string PageName
    {
        get
        {
            return null;
        } 
    }
    public string Permission
    {
        get
        {
            switch (PageName)
            {
                case "Page1":
                    return "Read";
                case "Page2":
                    return "Write";
                default:
                    return "None";
            }
        }
    }
}

And derived classes just for example:

public class Page1 : PageContext
{
    public override string PageName
    {
        get
        {
            return "Page1";
        }
    }
}
public class Page2 : PageContext
{
    public override string PageName
    {
        get
        {
            return "Page2";
        }
    }
}
public class Page3 : PageContext
{ 
}

And at the end you can use something like this:

string p1Permission = (new Page1()).Permission;//Read
string p2Permission = (new Page2()).Permission;//Write
string p3Permission = (new Page3()).Permission;//None

Hope will help.

Upvotes: 1

Related Questions