Iterating Controls
-
Hello all, I'm trying to implement a "user rights" system in our web application. I've done a few underpinnings but I'd like to centralize it. The following is the code I use:
public void EnforcePerms(Control parent, DataTable pSet){ foreach (Control c in parent.Controls) { if(c is LinkButton){ LinkButton lbc = (LinkButton)c; if(DBContain(lbc.ID, pSet)){ lbc.Enabled = false; lbc.BackColor = Color.Gainsboro; // lbc.Attributes.Add("class","rightsblock"); } } if(c is HyperLink){ HyperLink hpc = (HyperLink)c; if(DBContain(hpc.ID, pSet)){ hpc.Enabled = false; hpc.BackColor = Color.Gainsboro; // lbc.Attributes.Add("class","rightsblock"); } } if (c.Controls.Count > 0) { EnforcePerms(c, pSet); } } }
The problem is that I have to paste this on each page because it's recursive. I'd like to have a central class though that takes a ref parameter to the controls collection on an ASP.NET page and then enforces the permissions in a central location. Any suggestions? Has anyone who has done this before tell me if there's a better way? *->>Always working on my game, teach me *->>something new. cout << "dav1d\n"; -
Hello all, I'm trying to implement a "user rights" system in our web application. I've done a few underpinnings but I'd like to centralize it. The following is the code I use:
public void EnforcePerms(Control parent, DataTable pSet){ foreach (Control c in parent.Controls) { if(c is LinkButton){ LinkButton lbc = (LinkButton)c; if(DBContain(lbc.ID, pSet)){ lbc.Enabled = false; lbc.BackColor = Color.Gainsboro; // lbc.Attributes.Add("class","rightsblock"); } } if(c is HyperLink){ HyperLink hpc = (HyperLink)c; if(DBContain(hpc.ID, pSet)){ hpc.Enabled = false; hpc.BackColor = Color.Gainsboro; // lbc.Attributes.Add("class","rightsblock"); } } if (c.Controls.Count > 0) { EnforcePerms(c, pSet); } } }
The problem is that I have to paste this on each page because it's recursive. I'd like to have a central class though that takes a ref parameter to the controls collection on an ASP.NET page and then enforces the permissions in a central location. Any suggestions? Has anyone who has done this before tell me if there's a better way? *->>Always working on my game, teach me *->>something new. cout << "dav1d\n";How about making a base class for your pages? Create a class that inherits from
System.Web.UI.Page
and includes your method. The other pages in your application would then inherit from your base page class. The final product would look something like:public class MyPageBase : System.Web.UI.Page
{
protected void EnforcePerms()
{
DataSet pSet = GetPSet();
foreach (Control in this.Controls)
{
// Do work }
}
}
// The rest of the pages would inherit from MyPageBase
public class WebForm1 : MyPageBase
{
private void Page_Load(object sender, System.EventArgs ea)
{ -
How about making a base class for your pages? Create a class that inherits from
System.Web.UI.Page
and includes your method. The other pages in your application would then inherit from your base page class. The final product would look something like:public class MyPageBase : System.Web.UI.Page
{
protected void EnforcePerms()
{
DataSet pSet = GetPSet();
foreach (Control in this.Controls)
{
// Do work }
}
}
// The rest of the pages would inherit from MyPageBase
public class WebForm1 : MyPageBase
{
private void Page_Load(object sender, System.EventArgs ea)
{