【问题标题】:Make reference to a Button in a repeater?引用中继器中的按钮?
【发布时间】:2013-12-02 12:00:42
【问题描述】:

我在转发器中有一个带有按钮控件的表单,我试图在我的代码中引用它,以便我可以在适当的地方禁用和启用它,但它无法识别。

我尝试了一些看起来像这样的东西:

Sub btnClickPrev(ByVal sender As Object, ByVal e As EventArgs)
    'Get the reference of the clicked button.
    Dim button As Button = CType(sender, Button)

    'Get the command argument
    Dim commandArgument As String = button.CommandArgument

    'Get the Repeater Item reference
    Dim item As RepeaterItem = CType(button.NamingContainer, RepeaterItem)

    'Get the repeater item index
    Dim index As Integer = item.ItemIndex
    item.Enabled = false

End Sub

但是 item.enabled 不像 btnPrev.enabled=false 那样工作

这就是我的按钮表单控件中的内容:

  asp:Button ID="btnPrev" Text="Previous" OnClick="btnClickPrev" CommandArgument = '1' runat="server"

任何帮助将不胜感激。谢谢

【问题讨论】:

    标签: asp.net forms button reference repeater


    【解决方案1】:

    问题是,RepeaterItem 没有您要更改的任何 Enable 属性。相反,循环遍历 RepeaterItem 中的所有控件并像这样禁用它们:

    Sub btnClickPrev(ByVal sender As Object, ByVal e As EventArgs)
        'Get the reference of the clicked button.
        Dim button As Button = CType(sender, Button)
    
        'Get the Repeater Item reference
        Dim item As RepeaterItem = CType(button.NamingContainer, RepeaterItem)    
    
        For Each con In item.Controls
            'Disable button
            If TypeOf con Is Button Then
                CType(con, Button).Enabled = False
            End If
    
            'Disable other controls if you want
            If TypeOf con Is CheckBox Then
                CType(con, CheckBox).Enabled = False
            End If
        Next
    
        'item.Enabled = False 'Wouldn't work
    
    End Sub
    

    【讨论】:

      猜你喜欢
      • 2013-09-19
      • 2013-11-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2010-11-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多