Hide button if gridview has rows.

I want to hide a button on a page if my gridview has rows...

I can't seem to get it right as once the button disappears it won't appear again if the gridview has rows.


I just want to show the button if there are 0 rows and hide the button if there are rows.

    Protected Sub gvGenQuestionnaire_RowDataBound(sender As Object, e As GridViewRowEventArgs) Handles gvGenQuestionnaire.RowDataBound


        If gvGenQuestionnaire.Rows.Count = 0 Then btnViewGenQuest.Visible = True

        If gvGenQuestionnaire.Rows.Count > 0 Then btnViewGenQuest.Visible = False

    End Sub

Open in new window

EdAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

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

Imran Javed ZiaConsultant Software Engineer - .NET ArchitectCommented:
If your grid is in updatepanel then make sure that button is in same updatepanle too, or use triggers if button is in other panel.

and you can make your code more readable with following change

Protected Sub gvGenQuestionnaire_RowDataBound(sender As Object, e As GridViewRowEventArgs) Handles gvGenQuestionnaire.RowDataBound


        If gvGenQuestionnaire.Rows.Count = 0 Then 
               btnViewGenQuest.Visible = True
         else
                btnViewGenQuest.Visible = False
        endif
    End Sub


or 

Protected Sub gvGenQuestionnaire_RowDataBound(sender As Object, e As GridViewRowEventArgs) Handles gvGenQuestionnaire.RowDataBound

               btnViewGenQuest.Visible = gvGenQuestionnaire.Rows.Count <= 0
         
    End Sub

Open in new window

0
Lokesh B RDeveloperCommented:
Hi,

You can do that in Page_Load as below after your GridView bind line.

Protected Sub Page_Load(sender As Object, e As EventArgs)

	If gvGenQuestionnaire.Rows.Count > 0 Then
		btnViewGenQuest.Visible = True
	Else
		btnViewGenQuest.Visible = False
	End If
End Sub

Open in new window

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
Kamal KhaleefaInformation Security SpecialistCommented:
i don't think that this event
gvGenQuestionnaire_RowDataBound

will run once there is no rows

run the code at page load
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.