Home All Groups Group Topic Archive Search About
Author
2 Dec 2005 3:50 PM
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

Author
2 Dec 2005 4:30 PM
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/default.asp?url=/library/en-us/cpguide/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
Show quoteHide quote
"mferg***@yahoo.com" wrote:

> 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
>
>
Author
5 Dec 2005 1:02 PM
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