Click here to Skip to main content
15,884,739 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more: , +
Well in our project we are having a requirement as to create a global class file, In each of our ASPX.cs page we have to pass the page controls (Page.Controls) to the global class file i mentioned above, and i need to disable or enable the controls in the class. This can be simply done in each of the ASPX.cs page as,

C#
but_edit.enable = false;
but_delete.enable = false;

*Where but_edit and but_delete are two buttons in the ASPX page.*

Instead of defining this in each of the pages, i need to create a global class, so that i can pass the controls (like Textbox,Imagebutton) of each page to that class. In ASPX page,

C#
<asp:ImageButton ID="but_edit" runat="server" ImageUrl="~/Images/Admin/editbtn.png"
                        OnClick="btnsavelocation_Click" CssClass="btn1" />
<asp:ImageButton ID="but_close" runat="server" ImageUrl="~/Images/Admin/deletebtn.png"
                        CssClass="btn2" OnClick="btnclose_Click" /> 

This is what i have done in each of the ASPX.cs page,

CSS
Authorization assign_auth = new Authorization();
assign_auth.ChangeControlStatus(Page.Controls);

where Authorization is the class and ChangeControlStatus is the the method. This is my code in the common class page,

C#
 public class Authorization
 {
  public void ChangeControlStatus(ControlCollection PageControls)
  {
    foreach (Control ctrl in PageControls)
    {
        string  controlType = (ctrl.GetType()).Name;

                if (ctrl is Button)
                {
                    Button but = (Button)ctrl;
                    but.Enabled = false;
                }
    }
  }
}

And now,here its possible to enable or enable a control,like all Imagebuttons in the page. but is it possible to enable or disable a control based on the id (since I have specified the same id in all pages in our project)
The main reason for this is,in our project where there are users with many roles who can access the same pages,based on the role some can have access to EDIT and not to DELETE so need to work on this...
Hope I explained clearly..
Posted
Updated 5-Jul-13 23:15pm
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900