【问题标题】:Loop Through Controls on Web User Control循环通过 Web 用户控件上的控件
【发布时间】:2013-02-13 19:00:20
【问题描述】:

我在循环浏览用户控件上的控件时遇到问题。

我尝试了以下代码,但无法找到用户控件上的复选框。 (你可以看到我之前的一些尝试,我已经注释掉了。)

    'For Each Ctrl As Control In Page.Controls
    'For Each Ctrl As Control In Me.Page.Controls
    'For Each ctrl As Control In Request.Form
     '''Dim frm As Control = Me.FindControl("frmDefault")
     '''For Each Ctrl As Control In frm.Controls

    Dim Check As CheckBox

    For Each Ctrl As Control In Me.Controls
        If TypeOf Ctrl Is CheckBox Then
            Check = Ctrl
            ' Do something here...
        End If
    Next

用户控件上有多个复选框。上面显示的代码位于用户控件的代码隐藏页面上。

(用户控件与我的 CMS Sitecore 一起使用。我不确定这是否对我遇到的问题有任何影响。)

有什么建议吗?

【问题讨论】:

  • 如果调试,Controls集合的内容是什么?复选框是用户控件的直接子级还是包含在另一个服务器控件中?

标签: asp.net vb.net sitecore


【解决方案1】:

Sitecore 对遍历控件集合没有影响,这应该是可能的。

您是否循环通过正确的 Control-Collection? Me.Controls 是您的 Page-、UserControl- 或 RepeaterItems-Control 集合(或其他集合)吗? 如果复选框嵌套在另一个控件中,则需要步行到该控件集合。

也许您应该添加您的 .ascx 代码,以便我们可以看到您的控件集合的样子。

【讨论】:

    【解决方案2】:

    这会显示复选框的名称吗?

    For Each Ctrl As Control In Me.Controls
        If TypeOf Ctrl Is CheckBox Then
              MsgBox(Ctrl.Name)
        End If
    Next
    

    这应该让您知道您是否点击了复选框。如果不重新检查您的页面设计。

    我相信你应该没有问题分配 ctrl 来检查,它应该充当 ctrl 的指针。如果页面上有多个复选框,请针对 ctrl.name 执行 if 语句以获取正确的复选框。

    【讨论】:

      【解决方案3】:

      我终于弄清楚发生了什么。

      我在不同的表格中有复选框。这些表包含 runat="server"。该表位于一个也包含 runat="server" 的 Div 标记内。

      因此,我的代码永远找不到复选框。我必须添加一个 For Each 循环遍历 Div 标记并找到适当的表。然后我必须遍历表格才能找到复选框。

      【讨论】:

        【解决方案4】:

        您的某些控件具有控件。您的循环将忽略这些控件。我有一些用于获取所有控件的扩展方法(您可以指定 CheckBox 类型,因此您无需在调用代码中进行类型检查)

        <Extension()> _
        Public Function ChildControls(ByVal parent As Control) As List(Of Control)
            Return ChildControls(Of Control)(parent)
        End Function
        
        <Extension()> _
        Public Function ChildControls(Of T As Control)(ByVal parent As Control) As List(Of T)
            Dim result As New ArrayList()
            For Each ctrl As Control In parent.Controls
                If TypeOf ctrl Is T Then result.Add(ctrl)
                result.AddRange(ChildControls(Of T)(ctrl))
            Next
            Return result.ToArray().Select(Of T)(Function(arg1) CType(arg1, T)).ToList()
        End Function
        

        【讨论】:

          【解决方案5】:

          好吧,我解决了它如下。 (c#)

           foreach (Control c in Page.Controls)
                  {
                      foreach (Control childc in c.Controls)
                      {
                          if (childc.ClientID == "menupadraolateral1")
                          {
                              foreach (Control cMnuLat in childc.Controls)
                              {
          
                                  //here you can access the controls of usercontrol                           
          
                              }
          
          
                          }
                      }
                  }
          

          其中“menupadraolateral1”是调用用户控件时使用的 ID

          希望对你有所帮助

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2011-04-19
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2011-11-01
            • 1970-01-01
            相关资源
            最近更新 更多