Refreshing DataGrid in page_load

  • Thread starter Thread starter A.M
  • Start date Start date
A

A.M

Hi,

I want to refresh my DataGrid data on every postback.
I have following code in Page_Load event, but after first page load, the
DataGrid never gets refresh.

Here is the code i have in Page_load (it works at first page_load):

Dim DS As DataSet
Dim MyConnection As System.Data.SqlClient.SqlConnection
Dim MyCommand As System.Data.SqlClient.SqlDataAdapter

MyConnection = New
System.Data.SqlClient.SqlConnection(ConfigurationSettings.AppSettings("ConnS
tr"))
MyCommand = New System.Data.SqlClient.SqlDataAdapter("SELECT * FROM tblLog",
MyConnection)

DS = New DataSet
MyCommand.Fill(DS, "Log")

MyDataGrid.DataSource = DS.Tables("Log").DefaultView
MyDataGrid.DataBind()



Thanks,
Ali
 
Do you have the code incapsulated in a:

If Not IsPostBack Then conditional? If so you would need to take the Fill
and DataBind code outside of the conditional as well.

Just a thought,
Aaron
 
Hi Ali,

Thank you for posting to the MSDN newsgroups.

Based on my research and experience, I suggest you try the following
walkthrough on your side. Can you reproduce the same problem with it?

Walkthrough: Using a DataGrid Web Control to Read and Write Data
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/vbcon/html/
vbwlkwalkthroughusingdatagridwebcontroltoreadwritedata.asp

I hope it helps.

Best regards,

Jacob Yang
Microsoft Online Partner Support
Get Secure! ¨C www.microsoft.com/security
This posting is provided "as is" with no warranties and confers no rights.
 
Does the button trigger a postback? You can check that by putting a break point at the databind statement.

Is the Datagrid property EnableViewState set =true? That could cause your symptoms.
 
The button does trigges postback, and EnableViewState set =False (not true)

I disabled EnableViewState to have datagrid updated on every postback.

Thanks
Ali

Gavin Jacobs said:
Does the button trigger a postback? You can check that by putting a break
point at the databind statement.
 
Hi Ali,

I am trying to reproduce the problem on my side now. Would you please post
the code of the button click? I certainly appreciate your time.

Best regards,

Jacob Yang
Microsoft Online Partner Support
Get Secure! ¨C www.microsoft.com/security
This posting is provided "as is" with no warranties and confers no rights.
 
Hi Ali,

Based on my research and experience, I would like to share the following
information with you.

This issue exists because the data binding to the datagrid (in page_load
event in this case) occurs before the event that responses to the postback
event (button_click event in this case). That is, when the data binding
occurs, the update to the database has not happened. In other words, the
data bound to the datagrid was still the old data.

The page lifecycle on every request is as follows:

1. Instantiate
2. Initialize
3. TrackViewState
4. LoadViewState (postback)
5. Load postback data (postback, IPostBackDatahandler.LoadPostdata)
6. Load
7. Load postback data for dynamical controls added on Page_Load
8. Raise Changed Events (postback,
IPostBackDatahandler.RaisePostDataChanged)
9. Raise postback event (postback,
IPostBackEventHandler.RaisePostBackEvent)
10.PreRender
11. SaveViewState
12. Render
13. Unload
14. Dispose

According to the page lifecycle, the data binding occurs in step 6,
however, the data changing to the database occurs in step 9. The changes
can only be reflected to the datagrid in next postback.

To overcome this problem, we should re-do the data bind after the data
changing was submitted to the database. In this case, we can do it in the
end of the button_click event.

In addition, to avoid the unnecessary data binding, we should encapsulate
the data binding operation in page_load event inside the IsPostBack
conditional statement.

For more info on page lifecycle, please refer to the article at
http://aspalliance.com/articleViewer.aspx?aId=134&vId=&pId=

Does it answer your question? If I have misunderstood your concern, please
feel free to let me know.

Best regards,

Jacob Yang
Microsoft Online Partner Support
Get Secure! ¨C www.microsoft.com/security
This posting is provided "as is" with no warranties and confers no rights.
 
Back
Top