.Net Datagrid contents lost on postback

J

Jeff

Is there a workaround for datagrid contents being lost on postback?
All other controls persist. It's just datagrids that appear to be
reinitialized.
It doesn't make sense to continually rebuild datagrids on postback when
other controls don't require it.

Thanks in advance.
 
T

Teemu Keiski

Hi,

you need to show (code) how it is used. Otherwise it is bit hard to help
you.
 
S

sam

You've probably disabled viewstate so that you have to rebind (not
rebuild) your datagrid. That is very common and there's nothing wrong
with it. Unless you want to turn viewstate on and have a huge
viewstate.

Other controls dont' require it because they are using form field data
to repopulate. The datagrid can't do that.

I dont' know if this is what your question means.

-Sam
 
B

Brandon Driesen

Possibilities:

1. ViewState of the datagrid is disabled (unlikely)
2. DataBinding of the datagrid occurring at every page load (and not in the
initial load) (very likely)
 
J

Jeff

sam said:
You've probably disabled viewstate so that you have to rebind (not
rebuild) your datagrid. That is very common and there's nothing wrong
with it. Unless you want to turn viewstate on and have a huge
viewstate.

Other controls dont' require it because they are using form field data
to repopulate. The datagrid can't do that.

I dont' know if this is what your question means.

-Sam
 
J

Jeff

sam said:
You've probably disabled viewstate so that you have to rebind (not
rebuild) your datagrid. That is very common and there's nothing wrong
with it. Unless you want to turn viewstate on and have a huge
viewstate.

Other controls dont' require it because they are using form field data
to repopulate. The datagrid can't do that.

I dont' know if this is what your question means.

-Sam
Thanks Sam for the input. Tried turning viewstate on and off and same
problem. Code is significant so I didn't include it. Tried
initializing datagrid on page load with no avail. Still resets.
Something funny, one column is still populated. All others are
cleared.
Thanks again for your quick response.
 
J

Jeff

My code is basically this:
Private Sub Page_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load

If Not Page.IsPostBack() Then

'POPULATE THE DROPDOWN LISTS
Dim pdmWS As pdmUsers.pdmUsersV1R0 = New
pdmUsers.pdmUsersV1R0
sXML = pdmWS.GetMyDatabases(ErrorCode, ErrorMsg)
CreateCheckBoxLists(sDSN)
InitializeDatagrid()
dgResults.Visible = True
Else '(POSTBACK)
dgResults.Visible = False
If Not Request.Form("btnSave") Is Nothing Then
Dim pdmAdminWS As pdmAdmins.pdmAdminsV1R0 = New
pdmAdmins.pdmAdminsV1R0
'loop throught data grid returned results to update database
For i = 0 To Request.Form.Count - 1
If Request.Form.Keys(i) Like "*dgResults*_DSN:*_DOC:*" Then
If pdmAdminWS.AddCatDocXref(ErrorCode, ErrorMsg,
....) Then
lblErrorMsg.Text = "Update Successful "
Else
lblErrorMsg.Text = ErrorCode & ": " & ErrorMsg
End If
End If
Next
'data grid is initialized here
dgResults.Visible = True
End If
If Not Request.Form("btnSubmit") Is Nothing Then
Dim pdmWS As pdmUsers.pdmUsersV1R0 = New
pdmUsers.pdmUsersV1R0
sXML = pdmWS.Search(ErrorCode, ErrorMsg, ...)
If ErrorCode = 0 Then
doc.LoadXml(sXML)
Dim reader As New XmlNodeReader(doc)
Dim ds As New DataSet
ds.ReadXml(reader)
reader.Close()
' FOUND SOME RESULTS
If (ds.Tables.Count > 0) Then
addChkBoxCol(dgResults) ' Adds checkbox to
datagrid
dgResults.DataSource = ds.Tables(0).DefaultView
dgResults.DataBind()
dgResults.Visible = True
End If
End If
End If

End Sub



Thank you so much for your assistance.
 
J

Jeff

I've initialized the grid in the page load with a dummy row. Displays
nicely.
Tried viewstate on and off. No difference.
Thank you kindly for your assistance!
 
A

amit.phadke

Is autogeneratecolumns set to false?
I hd a similar problem, and it turned out that, if this is set to
false, then you need to setup the columns (not the actual data binding)
on every page load (preferably during the init phase)
 
J

Jeff

Is autogeneratecolumns set to false?
I hd a similar problem, and it turned out that, if this is set to
false, then you need to setup the columns (not the actual data binding)
on every page load (preferably during the init phase)

Thanks for the quick input. Interesting!
I'm curious how you set up the columns without binding the grid since
the datasource is lost on postback? The grid has AutoGenerateColumns =
false.
Thank you again.
 
A

Amit

Well, if you have viewstate enabled, (i guess i shud hv asked this
earlier)
the data in your datagrid is all saved, but since the autogenerate
columns is false (columns were generated first time, when you did the
data binding), you dont see anything in ur grid

Now on the page postback, the data is still there (assuming viewstate
is enabled) but the column information is not saved, so you set the
columns again. The way I do it is as below

Page_Init()
{
// set up the column 1
BoundColumn obc = new BoundColumn();
obc.HeaderText = sHeaderText;
obc.DataField = sColumnName;
DataGrid1.Columns.Add(obc);

// similarly setup all the other columns
....
}

Page_Load()
{
If (! postback)
{
ds = some query
DataGrid1.dataSource = ds
DataGrid1.DataBind()
}
}

hope this helps
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,995
Messages
2,570,231
Members
46,820
Latest member
GilbertoA5

Latest Threads

Top