views:

28

answers:

3

For example, if i have on the aspx page:

<asp:PlaceHolder ID="tab_0" runat="server" Visible="false"></asp:PlaceHolder>
<asp:PlaceHolder ID="tab_1" runat="server" Visible="false"></asp:PlaceHolder>

and i want to access these properties in the code behind page using values from a configuration file for example

string enabledTabs = "0,1,2,3";

if there a way i can use reflection to set them to enabled or disabled e.g.

foreach(var id in enabledTabs.Split(',')) 
{
  // <use reflection to get the correct tab control>

  // Set property of the tab
  tab.Visible = true;
}

I could acheive the result i want by using a switch statement and setting the particular control property, but i'd like to use reflection to get the tab to make it cleaner.

Could anyone help?

Thanks!

+3  A: 

You don't need reflection. Use Page.FindControl:

foreach(var id in enabledTabs.Split(','))
{
    PlaceHolder control = (PlaceHolder)this.FindControl("tab_"+id));
    control.Visible = true;
}
Justin Niessner
+1 Just pass in the prefix `tab_` and append the number to form the ID.
James
how do you guys reply so quickly?! thank you very much!
Raj
+1  A: 
foreach(var id in enabledTabs.Split(',')) 
{      

    // Set property of the tab
    Page.FindControl("tab_" + id.ToString()).Visible = true;
}
rick schott
A: 

you can do:

Control tab = Control.FindControl("tab_"+id);

Nealv