By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
432,414 Members | 1,057 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 432,414 IT Pros & Developers. It's quick & easy.

Refreshing DataGrid in page_load

P: n/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(ConfigurationS ettings.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
Nov 17 '05 #1
Share this Question
Share on Google+
7 Replies


P: n/a
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
"A.M" <IH*******@sapm123.com> wrote in message
news:en**************@tk2msftngp13.phx.gbl...
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(ConfigurationS ettings.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

Nov 17 '05 #2

P: n/a
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/de...us/vbcon/html/
vbwlkwalkthroughusingdatagridwebcontroltoreadwrite data.asp

I hope it helps.

Best regards,

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

Nov 17 '05 #3

P: n/a
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.
"A.M" <IH*******@sapm123.com> wrote in message news:ud**************@TK2MSFTNGP12.phx.gbl...

Thank you Jacob for replay.

The article you refered me to, is not my case.

Here is may case:

I have a button and a datagrid in a page.
The button is outside the datagrid.
when i click the button, it chages the data behind the datagrid.
As i mentioned in my post I DO NOT CHECK IsPostBack to supply data for
datagrid. So Page_Load always supplay data for dataset and datagrid.

And my problem is:

After clicking the button, Datagrid remains unchanged. To update the
datagrid, I have to press IE5 refresh button.

The summary of my question:
How can i make a datagrid to refresh or re-read it's data.

Regards,
Ali

"Jacob Yang [MSFT]" <ji***@online.microsoft.com> wrote in message
news:Dp*************@cpmsftngxa06.phx.gbl...
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/de...us/vbcon/html/
vbwlkwalkthroughusingdatagridwebcontroltoreadwrite data.asp

I hope it helps.

Best regards,

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


Nov 17 '05 #4

P: n/a
A.M
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" <no*********@inhell.com> wrote in message
news:OQ**************@tk2msftngp13.phx.gbl...
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.

"A.M" <IH*******@sapm123.com> wrote in message

news:ud**************@TK2MSFTNGP12.phx.gbl...

Thank you Jacob for replay.

The article you refered me to, is not my case.

Here is may case:

I have a button and a datagrid in a page.
The button is outside the datagrid.
when i click the button, it chages the data behind the datagrid.
As i mentioned in my post I DO NOT CHECK IsPostBack to supply data for
datagrid. So Page_Load always supplay data for dataset and datagrid.

And my problem is:

After clicking the button, Datagrid remains unchanged. To update the
datagrid, I have to press IE5 refresh button.

The summary of my question:
How can i make a datagrid to refresh or re-read it's data.

Regards,
Ali

"Jacob Yang [MSFT]" <ji***@online.microsoft.com> wrote in message
news:Dp*************@cpmsftngxa06.phx.gbl...
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/de...us/vbcon/html/
vbwlkwalkthroughusingdatagridwebcontroltoreadwrite data.asp

I hope it helps.

Best regards,

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



Nov 17 '05 #5

P: n/a
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! 每 www.microsoft.com/security
This posting is provided "as is" with no warranties and confers no rights.

Nov 17 '05 #6

P: n/a
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...=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! 每 www.microsoft.com/security
This posting is provided "as is" with no warranties and confers no rights.

Nov 17 '05 #7

P: n/a
A.M
Jacob,

That helped me.

Thank you very much,
Ali

"Jacob Yang [MSFT]" <ji***@online.microsoft.com> wrote in message
news:OS**************@cpmsftngxa06.phx.gbl...
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...=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! 每 www.microsoft.com/security
This posting is provided "as is" with no warranties and confers no rights.

Nov 17 '05 #8

This discussion thread is closed

Replies have been disabled for this discussion.