2

For Each Control In Page.Header.Controls

How can I do something as above, at the moment getting the error "Control is a Type and Cannot be used as an expression"

The Complete Code is as follows

Try
        ' I only do this on my production servers, so I declare those here.'
        If Request.ServerVariables("server_name") = "www.myproductionurl.com" Then
            ' Allow scripts and css to logged in CMS users'

            Dim checkLogin As New Controls.Login
            If checkLogin.IsLoggedIn <> True Then
                For Each Control In Page.Header.Controls
                    If Control.GetType.Name = "EktronJsControl" Or Control.GetType.Name = "EktronCssControl" Or Control.GetType.Name = "EktronModalCss" Then
                        Page.Header.Controls.Remove(Control)
                    Else
                        ' Removes the extra bubble inline style stuff that wasn't put in a CSS.''
                        Dim litControl As LiteralControl = Control
                        If litControl.Text = Nothing Then
                            litControl.Text = ""
                        End If

                        ' Removing blank.css file'
                        Dim htmlLink As HtmlLink = Control
                        If htmlLink.Href = "/css/blank.css" Then
                            Page.Header.Controls.Remove(Control)
                        End If
                    End If
                Next
            End If
        End If
    Catch ex As Exception

    End Try`

5 Answers 5

2

Sadly... VB.NET compiler says you cannot use "Control" as variable name because "Control" is a type too!!

Just use another identifier :)

Sign up to request clarification or add additional context in comments.

Comments

1

Since "Control" is a Class, it can not be used as a variable name. Either change it to another variable name, or surround it in square brackets ... [Control]

The brackets will tell the compiler to treat it as a variable rather than class name.

Comments

1

Change Control variable name to something else.

For Each ctrl In Page.Header.Controls

or

For Each ctrl As Control In Page.Header.Controls

The error message says it all: The word Control is a Type so you need to use something else.

UPDATE in response to comments.

You cannot remove the controls while your using a For Each loop to iterate through them.

Consider changing the code to something like:

For i as Integer = Page.Header.Controls.Length -1 to 0 Step -1
    Dim ctrl As Control = CType(Page.Header.Control(i),Control)

    If ctrl.GetType.Name = "EktronJsControl" Or ctrl.GetType.Name = "EktronCssControl" Or ctrl.GetType.Name = "EktronModalCss" Then
        Page.Header.Controls.Remove(ctrl)
    Else

        Dim litControl As LiteralControl = ctrl
        If litControl.Text = Nothing Then
            litControl.Text = ""
        End If

    End If
Next

Or you could keep a reference to the controls to be removed and remove them after the loop.

Dim removables = New List(Of Control)

For Each ctrl In Page.Header.Controls
    If ctrl.GetType.Name = "EktronJsControl" Or ctrl.GetType.Name = "EktronCssControl" Or ctrl.GetType.Name = "EktronModalCss" Then
        removables.Add(ctrl)
    Else

        Dim litControl As LiteralControl = ctrl
        If litControl.Text = Nothing Then
            litControl.Text = ""
        End If

    End If
Next

For Each c In removables
    Page.Header.Controls.Remove(c)
Next

Also, its unlikely that ctrl will be able to be converted into a LiteralControl and an HtmlLink so you need to add extra checks to determine which it is.

7 Comments

Tried that but got the following error Collection was modified; enumeration operation may not execute. Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code. Exception Details: System.InvalidOperationException: Collection was modified; enumeration operation may not execute. Source Error: Line 230: End If Line 231: End If Line 232: Next Line 233: End If Line 234:
@StevieB - that's because you are trying to remove the control from Header.Controls while still iterating through them. You cannot remove the controls inside the For Each loop
@StevieB - You could keep a reference to the controls to be removed (say in another list) and then remove them all after your For Each loop. Or you could use a normal For loop going backwards (For i as Integer = Page.Header.Controls.Length -1 to 0 step -1) and remove them in there.
Hey geoff sorry but im very confused I just picked up this code from a forum to try something, I wasnt appear it didnt actually work
@StevieB - what part are you confused about and I will try help?
|
0

First, I'd like to say I'm sorry that you're working with Ektron :(. I feel your pain.

The syntax of your loop is wrong, you need to specify a variable for your loop. The syntax is:

For Each [variable] As [type] In [collection]

So, change your loop to:

For Each ctrl as Control In Page.Header.Controls

And change all the references for the variable Control in your code to be ctrl.

For more info, see the MSDN article on For Each.

2 Comments

vb.net is not case sensitive so this won't work. You need a completely different variable name.
Good call, shoulda known that.
0

Make a list of Controls to remove after you have enumerated then remove them.

 Dim controlsToRemove As New  List(Of Control)
 For Each ctrl In Page.Header.Controls
    If ctrl.GetType.Name = "EktronJsControl" Or ctrl.GetType.Name = "EktronCssControl" Or ctrl.GetType.Name = "EktronModalCss" Then
        Page.Header.Controls.Remove(ctrl)
    Else

        Dim litControl As LiteralControl = ctrl
        If litControl.Text = Nothing Then
            litControl.Text = ""
        End If

        Dim htmlLink As HtmlLink = ctrl
        If htmlLink.Href = "/css/blank.css" Then
            'Page.Header.Controls.Remove(ctrl)
             controlsToRemove.Add(ctrl) 
        End If
    End If

Next

For Each ctrlToRemove In controlsToRemove
    Page.Header.Controls.Remove(ctrlToRemove )
Next

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.