JonoBB
asked on
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
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
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..
ASKER
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?
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?
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..
ASKER
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?
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?
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Ahhh, brilliant - thank you very much for you help.
ASKER
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.