在GridView中访问单个行模板

时间:2014-09-12 19:33:21

标签: asp.net vb.net templates gridview

我在ListBox控件上的button中有一个TemplateField(名为ListBoxProperty)和gridview(btnDeselectAll),显示在{{1}的每一行上}。下面显示的代码是gridview中按钮控件背后的代码。此代码适用于TemplateField中每一行ListBox的每个gridview。我希望它只影响从中单击它的gridview行中的ListBox。我已经尝试了一些失败的事情,所以我没有包括我的失败 - 只是几乎给了我想要的东西。不能包含图片,我的名声很弱。

Protected Sub btnDeselectAll_Click(sender As Object, e As System.EventArgs)
    For Each Row As GridViewRow In GridView1.Rows
        Dim myListBox As ListBox = _
            CType(Row.FindControl("ListBoxProperty"), ListBox)
        For Each selectedItem As ListItem In myListBox.Items
            selectedItem.Selected = False
        Next
    Next
End Sub

1 个答案:

答案 0 :(得分:0)

按钮的NamingContainerGridViewRow,你就是。

Protected Sub btnDeselectAll_Click(sender As Object, e As System.EventArgs)
    Dim btn = DirectCast(sender, Button)
    Dim row = DirectCast(btn.NamingContainer, GridViewRow)
    Dim myListBox = DirectCast(row.FindControl("ListBoxProperty"), ListBox)
    For Each selectedItem As ListItem In myListBox.Items
        selectedItem.Selected = False
    Next
End Sub