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

All Courses

From novice to tech pro — start learning today.