DataGrid and ViewState

M

mfergu98

Hello: I have several dynamic datagrids being created with editable
templatecolumns. I have a button added at the end of each datagrid
which fires off an update event. The event fires fine, but I can't
seem to be able to retrieve the datagrid. I have tried placing it in
the ViewState, but it tells me that it must be serializable or to use
the TypeConverter. Being relatively new to this, I am not sure exactly
how to go about doing that.

What I want to do is have each datagrid that is created be able to
independently be updated by the buttons associated with them.

In the EventHandler for each button, I've tried:

DataGrid dgr = (DataGrid) Page.FindControl("dgr" + strDataGrid);
and
DataGrid dgr = (DataGrid) ViewState["dgr" + strDataGrid];

but neither finds the datagrid control.

I create each datagrid dynamically based on a selection from a listbox
and add them to the page:

DataGrid dgr = new DataGrid();
TemplateColumn tm9 = new TemplateColumn();
ColumnTemplate ct9 = new ColumnTemplate("txtExtraAdultCharge",
"textbox", "", "", 40);
tm9.HeaderText = "Ext Adt Chg";
tm9.ItemTemplate = ct9;
dgr.Columns.AddAt(7,tm9);

dgr.EnableViewState = true;
this.ViewState.Add("dgr" + i.ToString(), dgr);

Page.Controls[1].Controls.Add(dgr);
Page.Controls[1].Controls.Add(btnUpdate);

I may not even be going down the right path, but any help would be much
appreciated!!

Thank you,
Mark
 
P

Phillip Williams

Hi Mark,

I can offer a few observations that might help:

1) Dynamically loaded controls should be loaded during the Page
Initialization phase to maintain their ViewState upon postback:

http://msdn.microsoft.com/library/d...guide/html/cpconcontrolexecutionlifecycle.asp

http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dnaspp/html/viewstate.asp


2) In the case where you have to create the datagrid late within the page
lifecycle (e.g., while responding to the SelectedIndexChanged as in your
case) and still want to take advantage of ASP.NET managing the ViewState then
you might consider one of those 2 approaches:

a. Register the datagrid control on the webform (.aspx) and add a markup to
it within the page but set its Visible property to false. While responding
to events triggered by various controls on the page set its visible to true
or false.

b. If you have to create the datagrid while responding to an event then save
in the Session a variable that allows you to reload it within the Init event
handling upon postback, e.g.

//put a line like this when you create the datagrid
Session("WasDataGridLoaded") = true;

//in the init event handling of control2 put code similar to this
if (Page.IsPostBack)
{
if (Session("WasDataGridLoaded"))
{
bool WasDataGridLoaded = Session(“WasDataGridLoaded) as bool ;
if (WasControl2Loaded) { //recreate the datagrid }
}
}

I have a sample where I created the datagrid dynamically upon page.init
here: http://www.societopia.net/Samples/DataGridEventDelegates.aspx

And I have another one where i used the declarative style (and could have
turned the child datagrid to visible or not later during the page life cycle)

http://www.societopia.net/Samples/DataGrid_Hierarchy.aspx
--
HTH,
Phillip Williams
http://www.societopia.net
http://www.webswapp.com


Hello: I have several dynamic datagrids being created with editable
templatecolumns. I have a button added at the end of each datagrid
which fires off an update event. The event fires fine, but I can't
seem to be able to retrieve the datagrid. I have tried placing it in
the ViewState, but it tells me that it must be serializable or to use
the TypeConverter. Being relatively new to this, I am not sure exactly
how to go about doing that.

What I want to do is have each datagrid that is created be able to
independently be updated by the buttons associated with them.

In the EventHandler for each button, I've tried:

DataGrid dgr = (DataGrid) Page.FindControl("dgr" + strDataGrid);
and
DataGrid dgr = (DataGrid) ViewState["dgr" + strDataGrid];

but neither finds the datagrid control.

I create each datagrid dynamically based on a selection from a listbox
and add them to the page:

DataGrid dgr = new DataGrid();
TemplateColumn tm9 = new TemplateColumn();
ColumnTemplate ct9 = new ColumnTemplate("txtExtraAdultCharge",
"textbox", "", "", 40);
tm9.HeaderText = "Ext Adt Chg";
tm9.ItemTemplate = ct9;
dgr.Columns.AddAt(7,tm9);

dgr.EnableViewState = true;
this.ViewState.Add("dgr" + i.ToString(), dgr);

Page.Controls[1].Controls.Add(dgr);
Page.Controls[1].Controls.Add(btnUpdate);

I may not even be going down the right path, but any help would be much
appreciated!!

Thank you,
Mark
 
M

mfergu98

Phillip,

Thanks so much for your response! This seems a bit more involved than
what I would hope would be a simple casting issue. I will review and
try to sort out and repost with anything I find. Thanks again!!
--Mark
 

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,764
Messages
2,569,566
Members
45,041
Latest member
RomeoFarnh

Latest Threads

Top