DataGrid and ViewState

Discussion in 'ASP .Net Web Controls' started by mfergu98@yahoo.com, Dec 2, 2005.

  1. Guest

    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
    , Dec 2, 2005
    #1
    1. Advertising

  2. 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


    "" 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
    >
    >
    Phillip Williams, Dec 2, 2005
    #2
    1. Advertising

  3. Guest

    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
    , Dec 5, 2005
    #3
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Ben Rush
    Replies:
    2
    Views:
    507
    Ben Rush
    Dec 5, 2003
  2. Steph
    Replies:
    2
    Views:
    1,647
    heislord5
    May 11, 2011
  3. ujjc001
    Replies:
    0
    Views:
    1,363
    ujjc001
    Jul 27, 2005
  4. sck10
    Replies:
    6
    Views:
    902
    sck10
    Sep 1, 2006
  5. mark

    Viewstate errors... how do I get viewstate working?

    mark, Feb 20, 2004, in forum: ASP .Net Building Controls
    Replies:
    0
    Views:
    269
Loading...

Share This Page