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

.Net Datagrid contents lost on postback

P: n/a
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.

Nov 19 '05 #1
Share this Question
Share on Google+
10 Replies


P: n/a
Hi,

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

--
Teemu Keiski
ASP.NET MVP, AspInsider
Finland, EU
http://blogs.aspadvice.com/joteke
"Jeff" <jd****@bigfoot.com> wrote in message
news:11*********************@g14g2000cwa.googlegro ups.com...
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.

Nov 19 '05 #2

P: n/a
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

Nov 19 '05 #3

P: n/a
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)
"Jeff" <jd****@bigfoot.com> wrote in message
news:11*********************@g14g2000cwa.googlegro ups.com...
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.

Nov 19 '05 #4

P: n/a

sam wrote:
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


Nov 19 '05 #5

P: n/a

sam wrote:
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.

Nov 19 '05 #6

P: n/a
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.

Nov 19 '05 #7

P: n/a
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!

Nov 19 '05 #8

P: n/a
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)

Nov 19 '05 #9

P: n/a

am*********@gmail.com wrote:
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.

Nov 19 '05 #10

P: n/a
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

Nov 19 '05 #11

This discussion thread is closed

Replies have been disabled for this discussion.