使用数据网格外部的复选框来影响其内容

时间:2012-05-21 09:40:02

标签: asp.net vb.net datagrid checkbox

我在数据网格外使用Checkbox。当我选中复选框autopostback为true时,这将显示图像,但我无法使用该脚本访问datagrid中的图像。如果我在数据网格之外使用单独的图像,则脚本可以正常工作。如何检查数据网格的复选框输出然后在数据网格中显示图像时,如何才能找到此脚本?

我正在使用的脚本是

<script runat="server">

    Sub Check(sender As Object, e As EventArgs)
        If checkShowImages.Checked Then
                img.Visible = True

        Else
            img.Visible = False
        End If
    End Sub

</script>

2 个答案:

答案 0 :(得分:1)

试试这个(假设您的checkbox的ID是“CheckBox1”而且DataGrid的ID是“Datagrid1”)...

Sub Check(sender As Object, e As EventArgs)
    For Each r As DataGridItem In Datagrid1.Items
        Try
            r.FindControl("img").Visible = CheckBox1.Checked
        Catch ex As Exception

        End Try
    Next
end sub  

希望这会有所帮助。祝你好运。

答案 1 :(得分:0)

如果我理解正确,当您选择外部复选框时,您希望在GridView中显示图像。为此,您需要迭代遍历GridView的行,如下所示:

foreach (GridViewRow row in myGrid.Rows)
{
   Image myImage = row.FindControl("HiddenImage") as Image;

   //Hide or show image based on checkbox state
   myImage.Visible = checkShowImages.Checked;       
}
VB(有点生疏):

For Each row As GridViewRow In myGrid.Rows
    Dim myImage As Image = TryCast(row.FindControl("HiddenImage"), Image)

    'Hide or show image based on checkbox state
    myImage.Visible = checkShowImages.Checked
Next