Change control state from db value.

jfsedlar3rd used Ask the Experts™
I am working on an ASP.NET site. My site has a complex model for the state of the controls depending on the status the record is in. I have multiple AD groups linked to the respective status that they can access.  I am adding another layer to the linker table which will store an ID of a linker table that holds the Control and the state. From code how can I enable,disable and readonly the control without building a huge case statement which contains every control name. Is there a class model i can use that will take the name of the control and the state i want the control to be in from the db?
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®

Why not just sue the Page.FindControl() and/or Control.FindControl() methods?

Then for each database record, you call findcontrol using the value from your data row's id column and set the appropriate value on that control.


I think I can make this work thank you. But I will still have to have a case statement for control type.

Select Caset db.ControlType

Case TextBox

 CType(Page.FindControl("ControlName"), TextBox).Visible = False
Case Button

 CType(Page.FindControl("ControlName"), Button).Visible = False
Case DropDownList

 CType(Page.FindControl("ControlName"), DropDownList).Visible = False
End Select


Actually that wont work cause the property e.g. visible,readonly etc can change too.
I think you can get around the first issue by doing something like:

Dim ctrl As Object = CType(Page.FindControl("ControlName"), GetType("TypeName"))

Where typename comes from your database. Dealing with the properties will be a little tickier I think. You will probably need to use Reflection to get the property. Look up the System.Refelection namespace and using reflection to access public properties. I'm not familiar enough with it to type it out from my head and I don't have time to load up visual studio and test it out right now.

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial