我有 asp:panel 和一些控件,标记如下
<asp:panel id="panel1" runat="server">
<asp:Label runat="server" Text="aaaa"></asp:Label>
<asp:Label runat="server" Text="bbbb"></asp:Label>
<asp:Label runat="server" Text="cccc"></asp:Label>
<asp:TextBox runat="server" ID="txt1"></asp:TextBox>
<asp:TextBox runat="server" ID="txt2"></asp:TextBox>
<asp:ImageButton ID="ibtn1" runat="server"/>
<asp:ImageButton ID="ibtn2" runat="server"/>
</asp:panel>
现在我想禁用所有控件,但启用 ibtn1 和 ibtn2
Protected Sub Page_Load(sender As Object, e As System.EventArgs) Handles Me.Load
panel1.Enabled = False
End Sub
Protected Sub Page_PreRender(sender As Object, e As System.EventArgs) Handles Me.PreRender
ibtn1.Enabled = True
ibtn2.Enabled = True
End Sub
所有控件都被禁用,这很好,但不适用于 ibtn1 和 ibtn 2。
然后,我尝试了这种方法
Public Sub lDisableAllChildControls(ByRef p As WebControl)
For Each c As System.Web.UI.WebControls.WebControl In p.Controls
c.Enabled = False
'recurse
lDisableAllChildControls(c)
Next
End Sub
但它给了我这个错误:
无法将“System.Web.UI.LiteralControl”类型的对象转换为“System.Web.UI.WebControls.WebControl”类型
有没有人有任何想法来完成这项工作?谢谢!