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
jcook32Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
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
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
ASP.NET

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.