[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 347
  • Last Modified:

Setting properties for controls

I have an asp.net page that I want to set all of the dropdownlist  properties with a button click. I have the following code and this is the error I am getting.

Dim ctlControl As Control
        For Each ctlControl In Page.Controls
            If ctlControl Is Control.DropDownList Then
                ctlControl.Enabled = False
            End If
        Next

Here is the error:

Compiler Error Message: BC30456: 'DropDownList' is not a member of 'System.Web.UI.Control'.

I am not sure what I am doing wrong or if I am even going at it the right way.

Thanks
0
ryapos
Asked:
ryapos
2 Solutions
 
TorrwinCommented:
You could try:

Select Case ctlControl.GetType
     Case DropDownList
          <your code here>
End Select
0
 
TimCotteeCommented:
Hi ryapos,

        Dim ctlControl As WebControl
        For Each ctlControl In Page.Controls
            If ctlControl.GetType Is GetType(DropDownList) Then
                ctlControl.Enabled = False
            End If
        Next

Tim Cottee
0
 
ryaposAuthor Commented:
Hi Tim,

This is the error I get when I try it

 Unable to cast object of type 'System.Web.UI.LiteralControl' to type 'System.Web.UI.WebControls.WebControl'.
0
 
DBAduck - Ben MillerPrincipal ConsultantCommented:
You should be able to just change TimCottee's suggestion to use Control instead of WebControl

        Dim ctlControl As Control
        For Each ctlControl In Page.Controls
            If ctlControl.GetType Is GetType(DropDownList) Then
                ctlControl.Enabled = False
            End If
        Next

LiteralControls are just Controls and WebControls are those with a UI element.

Ben.
0

Featured Post

[Webinar] Cloud and Mobile-First Strategy

Maybe you’ve fully adopted the cloud since the beginning. Or maybe you started with on-prem resources but are pursuing a “cloud and mobile first” strategy. Getting to that end state has its challenges. Discover how to build out a 100% cloud and mobile IT strategy in this webinar.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now