我得到了一个 aspx 页面中所有控件的通用列表。我试图对该列表的每个元素执行的操作是为这些 aspx 控件的属性 Visible 和 Enabled 设置一个值。
我不知道如何从通用列表的每个元素中获取 TYPE。
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
编辑 1:
如果我尝试这样做:
For Each ctl As Control In controlList
Response.Write(ctl.ID & "<br/>")
CType(Me.FindControl(ctl.ID), **ctl.GetType()** ).Enabled = True
Next
我收到错误:类型“ctl.GetType”未定义。
如果我尝试这样做:
For Each ctl As Control In controlList
Response.Write(ctl.ID & "<br/>")
CType(Me.FindControl(ctl.ID), **WebControl** ).Enabled = True
Next
我收到错误:无法将对象类型“System.Web.UI.HtmlControls.HtmlForm”转换为类型“System.Web.UI.WebControls.WebControl”
编辑 2:
如果我尝试这样做:
For Each ctl As Control In controlList
Response.Write(ctl.ID & "<br/>")
CType(ctl, WebControl).Enabled = False
Next
我收到错误:无法将对象类型“System.Web.UI.HtmlControls.HtmlForm”转换为类型“System.Web.UI.WebControls.WebControl”
我不知道我在这里错过了什么。