循环控制asp.net [英] Loop through controls asp.net

查看:65
本文介绍了循环控制asp.net的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试创建一个功能来启用禁用特定页面中的所有控件。



当我循环时,所有控件都被禁用,除了里面的控件div设置为runat =server



这是设计的一般视图:



 <  表格    id   =  form1      runat   =  server >  
< div id = 包装器 >
<% - 第一组ASP控件 - %>
< div id = 主要 runat = < span class =code-keyword> server >
<% - 第二套控件ASP - %>
< / div >
< / div >
< 表格 >





我的代码如下:



 对于 每个 c 作为控制 Page.Controls 
对于 每个 ctrl 作为控制 c。控制
' 禁用控件
下一步
下一步





我想要在我的所有页面中使用此功能,pl你能让我知道如何循环运行runat =Server的div吗?

解决方案

你看过这个链接使用简单代码启用或禁用页面中的所有控件 [ ^ ]



问候,

Pavan N


使用下面的代码,我将不仅可以禁用页面的子项,还可以禁用作为页面子项的控件子项的控件

 私有  Sub  DisableChildControls(controls  As  ControlCollection)

对于 每个 ctrl 作为控制中控制
' 我禁用了我要禁用的内容 - 例如

如果 TypeOf ctl TextBox 然后

DirectCast (ctl,TextBox).Enabled = False

ElseIf TypeOf ctl 按钮然后

DirectCast (ctl,Button).Enabled = False

ElseIf TypeOf ctl RadioButton 然后

DirectCast (ctl,RadioButton).Enabled = False

ElseIf < span class =code-keyword> TypeOf ctl RadioButtonList 然后

DirectCast (ctl,RadioButtonList).Enabled = False

ElseIf TypeOf ctl CheckBox 然后

DirectCast (ctl,CheckBox).Enabled = 错误

ElseIf TypeOf ctl CheckBoxList 然后

DirectCast (ctl,CheckBoxList).Enabled = False

ElseIf TypeOf ctl DropDownList 然后

DirectCast (ctl,DropDownList).Enabled = False

ElseIf TypeOf ctl ListBox 然后

DirectCast (ctl,ListBox).Attributes.Add(< span class =code-string> disabled disabled

结束 如果

如果 ctrl.Controls.Count> 0 然后
DisableChildControls(ctrl.Controls)
结束 如果
下一步
结束 Sub



在所需的活动中,我致电遍历使用:

 如果 。 Controls.Count>  0  然后 
DisableChildControls( Me .Controls)
结束 如果


I'm trying to make a function to enable disable all controls in a specific page.

When I loop through, all controls are disabled except the ones inside the div that is set to runat="server"

This is a general view of the design:

<form id="form1"  runat="server">
   <div id="wrapper">
   <%-- 1st set of ASP controls --%>
      <div id="Main"  runat="server">
         <%-- 2nd set ASP of controls --%>
      </div>
   </div>
<form>



My code looks like this:

For Each c As Control In Page.Controls
   For Each ctrl As Control In c.Controls
      'disabling controls 
   Next
Next



I want to use this function in all my pages, please could you let me know how to loop through the divs that are runat="Server"?

解决方案

Have you looked this link Enable or disable all controls in the page using simple code[^]

Regards,
Pavan N


By using this code below, I will be able to disable the not only the children of the page but also controls that are children of controls that are children of the page

Private Sub DisableChildControls(controls As ControlCollection)

        For Each ctrl As Control In controls
           'I disable what I want to disable -- for example

           If TypeOf ctl Is TextBox Then

                DirectCast(ctl, TextBox).Enabled = False

            ElseIf TypeOf ctl Is Button Then

                DirectCast(ctl, Button).Enabled = False

            ElseIf TypeOf ctl Is RadioButton Then

                DirectCast(ctl, RadioButton).Enabled = False

            ElseIf TypeOf ctl Is RadioButtonList Then

                DirectCast(ctl, RadioButtonList).Enabled = False

            ElseIf TypeOf ctl Is CheckBox Then

                DirectCast(ctl, CheckBox).Enabled = False

            ElseIf TypeOf ctl Is CheckBoxList Then

                DirectCast(ctl, CheckBoxList).Enabled = False

            ElseIf TypeOf ctl Is DropDownList Then

                DirectCast(ctl, DropDownList).Enabled = False

            ElseIf TypeOf ctl Is ListBox Then

                DirectCast(ctl, ListBox).Attributes.Add("disabled", "disabled")

            End If

          If ctrl.Controls.Count > 0 Then
                DisableChildControls(ctrl.Controls)
            End If
        Next
    End Sub


In the desired event, I call the traversal using:

If Me.Controls.Count > 0 Then
    DisableChildControls(Me.Controls)
End If


这篇关于循环控制asp.net的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆