【问题标题】:asp.net/VB.net: FindControl, instead of by ID by ControlTypeasp.net/VB.net:FindControl,而不是按 ControlType 的 ID
【发布时间】:2012-02-17 11:34:19
【问题描述】:

我需要在我的 asp.net 应用程序的转发器中找到一个控件。

目前我正在使用FindControl("IdOfControl"),而且效果很好。

但我需要按类型查找控件 (ImageButton)。

我当前的代码:

For Each rptItem As RepeaterItem In myRepeater.Items
    Dim imgBtn As ImageButton = TryCast(rptItem.FindControl("myImageBtn"), ImageButton)
    AddHandler imgBtn.Click, AddressOf imgBtn_Click
Next

我正在寻找类似的东西:

For Each rptItem As RepeaterItem In myRepeater.Items
    Dim imgBtn As ImageButton = TryCast(rptItem.FindControl(TypeOf ImageButton), ImageButton)
    AddHandler imgBtn.Click, AddressOf imgBtn_Click
Next

有人可以帮忙吗?

【问题讨论】:

标签: asp.net vb.net types repeater findcontrol


【解决方案1】:

试试这个

您的要求

For Each ri As RepeaterItem In myRepeater.Items
    For Each cn As Control In ri.Controls
        If cn.[GetType]() = GetType(ImageButton) Then
            Response.Write("ss")
            Response.Write(vbLf)
        End If
    Next
Next

e.g
    For Each cn As Control In form1.Controls
        If cn.[GetType]() = GetType(ImageButton) Then
            Response.Write("ss")
        End If
    Next

【讨论】:

  • 谢谢。这引导我找到正确的代码。用这篇文章作为草稿检查我的答案以获得完整的工作代码。
【解决方案2】:

我不确定您的转发器是否会有包含 ImageButtons 的嵌套控件。因此,类似于以下递归代码:

Public Function FindControl(ByVal ParentControl As Control) As Control
        Dim ReturnedControl As New Control
        For Each CurrentControl As Control In ParentControl.Controls
            CurrentControl.[GetType]() = GetType(ImageButton) Then
                ReturnedControl = CurrentControl
                Exit For
            End If
            If (CurrentControl.HasControls) Then
                ReturnedControl = FindControl(CurrentControl)
            End If
        Next
        Return ReturnedControl
    End Function

上面的函数会在Repeater控件中找到你作为参数传入的第一个ImageButton。希望这可以帮助。

【讨论】:

    【解决方案3】:

    Sanjay Goswami 发布了一个很好的解决方案。

    我不得不改变

    If cn.[GetType]() = GetType(ImageButton) Then
    

    到

    If cn.GetType().Equals(GetType(ImageButton)) Then
    

    并添加我的东西。

    完整的工作代码:

    For Each rptItem As RepeaterItem In myRepeater.Items
         For Each cn As Control In rptItem.Controls
                If cn.GetType().Equals(GetType(ImageButton)) Then
                    AddHandler (TryCast(rptItem.FindControl(cn.ID), ImageButton)).Click, AddressOf imgBtn_Click
                End If
         Next
    Next
    

    【讨论】:

      【解决方案4】:
          For Each cn As Control In Me.Controls
              If (cn.[GetType]().Equals(GetType(Button))) Then
                  Dim str1 As String = cn.Text
                  ds = fobj.getrecord("select shopid from tbstallbooking where shopid='" + str1 + "'")
                  n = ds.Tables(0).Rows.Count
                  If (n > 0) Then
                      cn.BackColor = Color.Red
                      cn.Enabled = False
                      ds.Clear()
                  Else
                      cn.BackColor = Color.Green
                      cn.Enabled = True
                  End If
              End If
          Next
      

      【讨论】:

      • 我想在表单中找到控件并制作有助于这种方式的地图布局
      猜你喜欢
      • 2011-02-15
      • 2010-11-08
      • 2016-04-01
      • 1970-01-01
      • 2012-10-17
      • 1970-01-01
      • 1970-01-01
      • 2020-11-26
      • 2023-03-22
      相关资源
      最近更新 更多