Sophie
Sophie

Reputation: 865

Using a checkbox outside of a datagrid to affect its contents

I am using a Checkbox outside of a datagrid. When i select the check box autopostback is true, and this would then show the image, but i cant access the images within the datagrid with that script. If i use a seperate image outside of the datagrid the script works. How can i get this script to work finding when the checkbox out side of the datagrid is checked to then show the image within the datagrid?

The script i am using is

<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>

Upvotes: 2

Views: 316

Answers (2)

Krishanu Dey
Krishanu Dey

Reputation: 6406

Try this(Assuming your checkbox's id is "CheckBox1" & DataGrid's Id is "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  

Hope this helps. Good luck.

Upvotes: 1

Surinder Bhomra
Surinder Bhomra

Reputation: 2199

If I understand correctly, you have images in your GridView that you want to display when a checkbox outside is selected. To do this, you will need to iterate through the rows of your GridView like so:

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 (a little rusty):

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

Upvotes: 0

Related Questions