asp.net/VB.net:FindControl,而不是按 ID 按 ControlType

发布于 2025-01-06 08:36:30 字数 644 浏览 1 评论 0原文

我需要在我的 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

有人可以帮忙吗?

i need to find a control in a repeater in my asp.net application.

At the moment I'm using FindControl("IdOfControl") and this is working well.

But I need to find a control by its type (ImageButton).

My current code:

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

I'm searching for something like that:

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

Can anybody help?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

旧伤慢歌 2025-01-13 08:36:30

试试这个

你的要求

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

Try this

your requirement

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
著墨染雨君画夕 2025-01-13 08:36:30

我不确定您的中继器是否有包含图像按钮的嵌套控件。因此,类似于以下递归代码:

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。希望这有帮助。

I am not sure if your repeater will have nested controls, that contains ImageButtons. So, something along the lines of the following recursive code:

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

The above function will find the first ImageButton in the Repeater control that you have passed in as a parameter. Hope this helps.

夏末的微笑 2025-01-13 08:36:30

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

Sanjay Goswami posted a good solution to work with.

I had to change the

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

to

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

and add my stuff.

Complete working code:

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
橘虞初梦 2025-01-13 08:36:30
    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
    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
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文