Link to home
Create AccountLog in
Avatar of miyahira
miyahiraFlag for Peru

asked on

Setting properties of aspx controls based in a generic List

I get a generic list of all controls in an aspx page. What I'm trying to do with each element of that list is to set a value for properties Visible and Enabled of those aspx controls.

I don't know how to get TYPE from each element of generic list.

Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

        Dim controlList As New List(Of Control)
        controlList = AddControls(Page.Controls, controlList)
        For Each ctl As Control In controlList
            Response.Write(ctl.ID & "<br/>")

            'Here I setup property Enabled of control textbox1
            CType(Me.FindControl("textbox1"), TextBox).Enabled = True

            'I'd like to setup property Enabled of control ctl.ID, but how to invoke <type of ctl>
            CType(Me.FindControl(ctl.ID), <type of ctl> ).Enabled = True

        Next
    End Sub

    Private Function AddControls(ByVal page As ControlCollection, ByVal controlList As List(Of Control)) As List(Of Control)
        For Each c As Control In page
            If c.ID IsNot Nothing Then
                controlList.Add(c)
            End If

            If c.HasControls() Then
                AddControls(c.Controls, controlList)
            End If
        Next
        Return controlList
    End Function

Open in new window

Avatar of Miguel Oz
Miguel Oz
Flag of Australia image

Replace:
 CType(Me.FindControl(ctl.ID), <type of ctl> ).Enabled = True

with:
 CType(Me.FindControl(ctl.ID), ctl.GetType() ).Enabled = True
Avatar of miyahira

ASKER

I tried what you suggest, but got error
Type 'ctl.GetType' is undefined.

I even tried:

 For Each ctl As Control In controlList
            Response.Write(ctl.ID & "<br/>")
            Dim obj As Type = ctl.GetType()
            CType(Me.FindControl(ctl.ID), obj).Enabled = False

Next

Open in new window


But got error: Type 'obj' is undefined.
ASKER CERTIFIED SOLUTION
Avatar of miyahira
miyahira
Flag of Peru image

Link to home
membership
Create a free account to see this answer
Signing up is free and takes 30 seconds. No credit card required.
See answer
Found solution by myself