对于Page.Header.Controls中的每个控件
我怎样才能做到如上所述,目前正在收到错误 “控制是一种类型,不能用作表达”
完整代码如下
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`
答案 0 :(得分:2)
可悲的是...... VB.NET编译器说你不能使用“Control”作为变量名,因为“Control”也是一种类型!!
只需使用其他标识符:)
答案 1 :(得分:1)
将Control
变量名称更改为其他名称。
For Each ctrl In Page.Header.Controls
或
For Each ctrl As Control In Page.Header.Controls
错误消息说明了一切:单词Control
是Type
,因此您需要使用其他内容。
更新以回应评论。
使用For Each
循环迭代它们时,无法删除控件。
考虑将代码更改为:
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
或者您可以保留对要删除的控件的引用,并在循环后删除它们。
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
此外,ctrl
不太可能转换为LiteralControl
和HtmlLink
,因此您需要添加额外的检查以确定它是什么。
答案 2 :(得分:1)
由于“Control”是一个类,因此不能用作变量名。将其更改为另一个变量名称,或将其括在方括号中... [控制]
括号将告诉编译器将其视为变量而不是类名。
答案 3 :(得分:0)
首先,我想说你很抱歉你和Ektron一起工作:(。我感到很痛苦。
循环的语法错误,您需要为循环指定变量。语法是:
For Each [variable] As [type] In [collection]
所以,将你的循环改为:
For Each ctrl as Control In Page.Header.Controls
并将代码中变量Control
的所有引用更改为ctrl
。
有关详细信息,请参阅For Each的MSDN article。
答案 4 :(得分:0)
列出要在枚举后删除的控件列表然后将其删除。
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