• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 329
  • Last Modified:

Show or hide a control based on the value of another control in a gridview

I have a gridview with one template column. in the column there are three controls. a link button, image button and hyperlink. when the dataset is returned if there is an image it will show the image or else it will show the linkbutton. that all works fine. What I want to do is if the Imagebutton url value is not nothing i want to show a hyperlink that i will pass a url string to. I added the code to the Gridview load control and this is the code:
      Dim gridviewrow As GridViewRow
        Dim imagebtn As New ImageButton

        gridviewrow = GridView1.SelectedRow
        'imagebtn = CType(gridviewrow.FindControl("Imagebutton1"), ImageButton)
        imagebtn = CType(GridView1.FindControl("Imagebutton1"), ImageButton) 'error on this line
        If imagebtn.ImageUrl = "" Then
        Else
            'CType(GridView1.FindControl("Hyperlink1"), HyperLink).Text = "see more..."
            'CType(GridView1.FindControl("Hyperlink1"), HyperLink).ImageUrl = "http://www.msn.com"
        End If

not sure if this is correct. the error is get is
Object reference not set to an instance of an object.
on this line:
imagebtn = CType(GridView1.FindControl("Imagebutton1"), ImageButton)

Please help
Thanks
jcook32
0
jcook32
Asked:
jcook32
  • 2
  • 2
1 Solution
 
klace06Commented:
Hi
I would try your code in the gridview rowdatabound event

like this

    GridView2_RowDataBound(object sender, GridViewRowEventArgs e)
        {  
        gridviewrow = e.Row
        'imagebtn = CType(gridviewrow.FindControl("Imagebutton1"), ImageButton)
        imagebtn = CType(GridView1.FindControl("Imagebutton1"), ImageButton) 'error on this line
        If imagebtn.ImageUrl = "" Then
        Else
            'CType(GridView1.FindControl("Hyperlink1"), HyperLink).Text = "see more..."
            'CType(GridView1.FindControl("Hyperlink1"), HyperLink).ImageUrl = "http://www.msn.com"

}
0
 
jcook32Author Commented:
still get the same "Object reference not set to an instance of an object. " error.

0
 
arhameCommented:
Two things, you probably have an imagebutton on each row right?  So I"d put that code under the row_databound event.  Then put the find controls as e.item.findcontrol("imagebutton1")

Also, put the code between:

If e.Row.RowType = DataControlRowType.DataRow Then

End If

So it doesn't try to find an image that doesn't exist on a header/footer row.
0
 
klace06Commented:
can i see your aspx file for the gridview, im sure i can sort it out if u show me that
0
 
jcook32Author Commented:
Thanks for the quick response. the if statement is what I was missing
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 2
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now