What is reflection?

Posted on Updated on

All .NET compilers produce metadata about the types defined in the modules they produce. This metadata is packaged along with the module (modules in turn are packaged together in assemblies), and can be accessed by a mechanism called reflection.
The System.Reflection namespace contains classes that can be used to interrogate the types for a module/assembly.

You can get this goal using ReflectionComponent in the App_Code folder by managing the parameter object that you pass when the static method ManagedControls.ChangeControlsAttributeValues is called. Let’s see how:

After you have selected areas, in “Enable by role”, select “Operator”. Note that each webcontrol in the controls container includes a custom attribute named Security. In this example, in the control, when the value of “Security” contains “Operator” the attribute “Enable” will be set to “true“.

<asp:Button ID="btnOne" runat="server" Security="Administrator;Operator;Other" Text="Button" Enabled="False" 
         OnClientClick="javascript:return false;"/>
<asp:Label ID="lblBtnOne" runat="server" Text="(Disabled)" ForeColor="Red"></asp:Label><br /><br />   

<asp:CheckBox ID="chkOne" runat="server" Security="Administrator;Operator" Text="CheckBox" Enabled="False"/>
<asp:Label ID="lblChkOne" runat="server" Text="(Disabled)" ForeColor="red"></asp:Label><br /><br />

<asp:TextBox ID="txtOne" runat="server" Security="Administrator" Enabled="False" ></asp:TextBox>
<asp:Label ID="lblTxtOne" runat="server" Text="(Disabled)" ForeColor="red"></asp:Label><br/><br/>

To get this goal you need to call the static method ManagedControls.ChangeControlsAttributeValues of the utility with the correct parameters. Look at the code in the ddlUserRoles_SelectedIndexChanged method.

_managedControlParameter = new ManagedControlParameter();

//Set container control
if (chklTemplate.Items[0].Selected)
    _managedControlParameter.MainControls.Add(Master);
if (chklTemplate.Items[1].Selected)
    _managedControlParameter.MainControls.Add(rightFieldset);
if (chklTemplate.Items[2].Selected)
    _managedControlParameter.MainControls.Add(firstUserControl);
if (chklTemplate.Items[3].Selected)
    _managedControlParameter.MainControls.Add(secondUserControl);

//Set type of control you want change property 
_managedControlParameter.ManagedControls.Add(ManagedControlType.All);

//Set property you want change if custom attribute (Security) is setting to correct value
_managedControlParameter.Attributes.Add(new ManagedControlTypeAttributesValue
                                            {
                                                Attribute = "Enabled",
                                                AttributeNewValue = "true",
                                                CustomAttribute = "Security",
                                                CustomAttributeValue = ddlUserRoles.SelectedValue
                                            });
//Call
ManagedControls.ChangeControlsAttributesValues(_managedControlParameter);
Advertisements

Leave a Reply

Please log in using one of these methods to post your comment:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s