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

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
0
JonoBB
Asked:
JonoBB
  • 4
  • 3
1 Solution
 
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.
0
 
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..
0
 
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?
0
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!

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

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

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