Perform action after gridview populated

Hi,

I have an asp.net 2.0 vb page that contains a single gridview. The sql select statement uses a textbox as the WHERE clause; i.e. it allows users to enter a free text string and search for records, which are then shown in the gridview. This all works 100%.

I also have a label on the page (called lblSort), that I would like to show if the gridview contains information. My code is as follows:

        If Me.GridView1.Rows.Count > 0 Then
            Me.lblSort.Visible = True
        Else
            Me.lblSort.Visible = False
        End If

Now, I have tried putting this code on various events, but it doesnt seem to work on the first postback.

For example, I tried putting a test button on the page, with the following code, but this required me to click the button *twice* for the label to appear or dissapear.

    Protected Sub btnTest_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnTest.Click
        If Me.GridView1.Rows.Count > 0 Then
            Me.lblSort.Visible = True
        Else
            Me.lblSort.Visible = False
        End If
    End Sub

I have also tried
    Protected Sub GridView1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles GridView1.Load
with the same results

So, my question is - on what event can I put my code so that it fires after the gridview has been populated.

Thanks for the help
LVL 8
JonoBBAsked:
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.

JonoBBAuthor Commented:
Well, I tried this and it works!

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        Me.GridView1.DataBind()
    End Sub

    Protected Sub GridView1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles GridView1.Load
        If Me.GridView1.Rows.Count > 0 Then
            Me.lblSort.Visible = True
        Else
            Me.lblSort.Visible = False
        End If
    End Sub

So, it would appear that the gridview needs to be bound each time the page loads.

It my logic above correct? Im keeping the points available for the person that can explain the workings behind this.
mirmansoorCommented:
the GridView1_Load() is called only after it is bound.. so the code u write in that sub will only fire after it is bound.. and since you are binding it on page load. it will bind the grid and then execute your code.. I hope this was clear for you..
JonoBBAuthor Commented:
So, then my question is :

If I didnt bind the gridview on the page load event, when would it bind?

Or is it that I'm really forcing the bind to happen twice; i.e. once automatically and once on the page load event?
Exploring SQL Server 2016: Fundamentals

Learn the fundamentals of Microsoft SQL Server, a relational database management system that stores and retrieves data when requested by other software applications.

mirmansoorCommented:
the gridview will not bind unless you explicitly call the gridview1.DataBind() function.. here you are binding the gridview on page load.. i dont know if you are calling it somewhere else as well.. but one thing is for sure.. the gridview will populate once it is bound.. ie calling the DataBind() function..
JonoBBAuthor Commented:
Sorry, being a bit slow here as this is new to me - if we assume that a gridview gets populated with data, then is it a correct assumption that the databind function must have been called. Put another way - if we dont databind a gridview, then its not gonna get any data. Right?

If thats correct, then howecome in VWD express, i can create a sqldatasource, create a gridview (with no where clause), run the page and the gridview is populated automatically (i.e. there is *no* code behind explicitly databinding the gridview)

Am I missing something?
mirmansoorCommented:
there is a property of GridView "AutoGenerateColumns" when it is set to true.. and in your case it is.. then it will populate the data with no code behind.. but in most other scenarios we have to set that property to false and populate the gridview with the databind() function. I guess i assumed in your case it is set to false.. my bad.. Anyway... i hope i'm clear now..

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
JonoBBAuthor Commented:
Ahhh, brilliant - thank you very much for you help.
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.