Adding a row without rebinding, reloading ViewState

Discussion in 'ASP .Net Datagrid Control' started by Oleg Ogurok, Jul 28, 2005.

  1. Oleg Ogurok

    Oleg Ogurok Guest

    Hi there,

    I'm looking for a way to add a row to DataGrid without binding the data
    source to it.

    In the DataGrid code, when DataBind() is called, ViewState is discarded and
    new set of controls is created with the values from the datasource.

    On a postback, DataGrid recreates the controls based on the values in
    ViewState["_!DataSourceItemCount"] and ViewState["_!ItemCount"].

    Let's say I have a button in a Header of one of the columns. The button has
    "AddNewRow" as its CommandName. I can then override OnItemCommand, catch the
    desired command and increment the _!DataSourceItemCount and _!ItemCount
    values in ViewState, as follows:

    protected override void OnItemCommand(DataGridCommandEventArgs e)

    {

    base.OnItemCommand (e);

    if (e.CommandName == "AddNewRow")

    {

    int itemCount = (int)ViewState["_!ItemCount"];

    ViewState["_!ItemCount"] = itemCount + 1;

    int dataSourceItemCount = (int)ViewState["_!DataSourceItemCount"];

    ViewState["_!DataSourceItemCount"] = dataSourceItemCount + 1;

    CreateChildControls();

    }


    }

    The probem is that even though I'm calling CraeteChaildControls() to
    recreate controls, the controls come out empty. This is because ViewState of
    those controls has already been restored. I'm wondering if there is a way to
    restore the ViewState for those controls once again.

    Thanks.
    -Oleg
    Oleg Ogurok, Jul 28, 2005
    #1
    1. Advertising

  2. Oleg Ogurok

    Brock Allen Guest

    That won't work because each control has it's own bucket of ViewState --
    the keys aren't shared across the page. IOW, you indexing into ViewState["ItemCount"]
    will be a different entry in ViewState than the DataGrid's ViewState["ItemCount"].

    Why don't you just use the DataGrid object and create a new row?

    -Brock
    DevelopMentor
    http://staff.develop.com/ballen



    > Hi there,
    >
    > I'm looking for a way to add a row to DataGrid without binding the
    > data source to it.
    >
    > In the DataGrid code, when DataBind() is called, ViewState is
    > discarded and new set of controls is created with the values from the
    > datasource.
    >
    > On a postback, DataGrid recreates the controls based on the values in
    > ViewState["_!DataSourceItemCount"] and ViewState["_!ItemCount"].
    >
    > Let's say I have a button in a Header of one of the columns. The
    > button has "AddNewRow" as its CommandName. I can then override
    > OnItemCommand, catch the desired command and increment the
    > _!DataSourceItemCount and _!ItemCount values in ViewState, as follows:
    >
    > protected override void OnItemCommand(DataGridCommandEventArgs e)
    >
    > {
    >
    > base.OnItemCommand (e);
    >
    > if (e.CommandName == "AddNewRow")
    >
    > {
    >
    > int itemCount = (int)ViewState["_!ItemCount"];
    >
    > ViewState["_!ItemCount"] = itemCount + 1;
    >
    > int dataSourceItemCount = (int)ViewState["_!DataSourceItemCount"];
    >
    > ViewState["_!DataSourceItemCount"] = dataSourceItemCount + 1;
    >
    > CreateChildControls();
    >
    > }
    >
    > }
    >
    > The probem is that even though I'm calling CraeteChaildControls() to
    > recreate controls, the controls come out empty. This is because
    > ViewState of those controls has already been restored. I'm wondering
    > if there is a way to restore the ViewState for those controls once
    > again.
    >
    > Thanks.
    > -Oleg
    Brock Allen, Jul 28, 2005
    #2
    1. Advertising

  3. Oleg Ogurok

    Oleg Ogurok Guest

    I'm getting ViewState["_!ItemCount"] inside a class derived from DataGrid
    (using C# inheritance), so I'm getting the right value. I can increment this
    value, and DataGrid will recognize it when rebuilding controls. However, I
    can't seem to figure out how to make the controls restore their ViewState
    values.

    I know it's possible to add a DataRow to a DataTable and call DataBind()
    again but I'm trying to avoid that.

    -Oleg.


    "Brock Allen" <> wrote in message
    news:...
    > That won't work because each control has it's own bucket of ViewState --
    > the keys aren't shared across the page. IOW, you indexing into

    ViewState["ItemCount"]
    > will be a different entry in ViewState than the DataGrid's

    ViewState["ItemCount"].
    >
    > Why don't you just use the DataGrid object and create a new row?
    >
    > -Brock
    > DevelopMentor
    > http://staff.develop.com/ballen
    >
    >
    >
    > > Hi there,
    > >
    > > I'm looking for a way to add a row to DataGrid without binding the
    > > data source to it.
    > >
    > > In the DataGrid code, when DataBind() is called, ViewState is
    > > discarded and new set of controls is created with the values from the
    > > datasource.
    > >
    > > On a postback, DataGrid recreates the controls based on the values in
    > > ViewState["_!DataSourceItemCount"] and ViewState["_!ItemCount"].
    > >
    > > Let's say I have a button in a Header of one of the columns. The
    > > button has "AddNewRow" as its CommandName. I can then override
    > > OnItemCommand, catch the desired command and increment the
    > > _!DataSourceItemCount and _!ItemCount values in ViewState, as follows:
    > >
    > > protected override void OnItemCommand(DataGridCommandEventArgs e)
    > >
    > > {
    > >
    > > base.OnItemCommand (e);
    > >
    > > if (e.CommandName == "AddNewRow")
    > >
    > > {
    > >
    > > int itemCount = (int)ViewState["_!ItemCount"];
    > >
    > > ViewState["_!ItemCount"] = itemCount + 1;
    > >
    > > int dataSourceItemCount = (int)ViewState["_!DataSourceItemCount"];
    > >
    > > ViewState["_!DataSourceItemCount"] = dataSourceItemCount + 1;
    > >
    > > CreateChildControls();
    > >
    > > }
    > >
    > > }
    > >
    > > The probem is that even though I'm calling CraeteChaildControls() to
    > > recreate controls, the controls come out empty. This is because
    > > ViewState of those controls has already been restored. I'm wondering
    > > if there is a way to restore the ViewState for those controls once
    > > again.
    > >
    > > Thanks.
    > > -Oleg

    >
    >
    >
    Oleg Ogurok, Jul 28, 2005
    #3
  4. Oleg Ogurok

    Brock Allen Guest

    Ok, cool that it's a derived class.

    But, ViewState is loaded between the OnInit and OnLoad events in the page.
    That's also about when the posted GridView rebuilds itself from the ViewState.
    You're modifying ViewState far to long after this stage (and I think you
    realize that). I suppose you could call CreateChildControls() on yourself
    to force a reload.

    -Brock
    DevelopMentor
    http://staff.develop.com/ballen



    > I'm getting ViewState["_!ItemCount"] inside a class derived from
    > DataGrid (using C# inheritance), so I'm getting the right value. I can
    > increment this value, and DataGrid will recognize it when rebuilding
    > controls. However, I can't seem to figure out how to make the controls
    > restore their ViewState values.
    >
    > I know it's possible to add a DataRow to a DataTable and call
    > DataBind() again but I'm trying to avoid that.
    >
    > -Oleg.
    >
    > "Brock Allen" <> wrote in message
    > news:...
    >
    >> That won't work because each control has it's own bucket of ViewState
    >> -- the keys aren't shared across the page. IOW, you indexing into
    >>

    > ViewState["ItemCount"]
    >
    >> will be a different entry in ViewState than the DataGrid's
    >>

    > ViewState["ItemCount"].
    >
    >> Why don't you just use the DataGrid object and create a new row?
    >>
    >> -Brock
    >> DevelopMentor
    >> http://staff.develop.com/ballen
    >>> Hi there,
    >>>
    >>> I'm looking for a way to add a row to DataGrid without binding the
    >>> data source to it.
    >>>
    >>> In the DataGrid code, when DataBind() is called, ViewState is
    >>> discarded and new set of controls is created with the values from
    >>> the datasource.
    >>>
    >>> On a postback, DataGrid recreates the controls based on the values
    >>> in ViewState["_!DataSourceItemCount"] and ViewState["_!ItemCount"].
    >>>
    >>> Let's say I have a button in a Header of one of the columns. The
    >>> button has "AddNewRow" as its CommandName. I can then override
    >>> OnItemCommand, catch the desired command and increment the
    >>> _!DataSourceItemCount and _!ItemCount values in ViewState, as
    >>> follows:
    >>>
    >>> protected override void OnItemCommand(DataGridCommandEventArgs e)
    >>>
    >>> {
    >>>
    >>> base.OnItemCommand (e);
    >>>
    >>> if (e.CommandName == "AddNewRow")
    >>>
    >>> {
    >>>
    >>> int itemCount = (int)ViewState["_!ItemCount"];
    >>>
    >>> ViewState["_!ItemCount"] = itemCount + 1;
    >>>
    >>> int dataSourceItemCount = (int)ViewState["_!DataSourceItemCount"];
    >>>
    >>> ViewState["_!DataSourceItemCount"] = dataSourceItemCount + 1;
    >>>
    >>> CreateChildControls();
    >>>
    >>> }
    >>>
    >>> }
    >>>
    >>> The probem is that even though I'm calling CraeteChaildControls() to
    >>> recreate controls, the controls come out empty. This is because
    >>> ViewState of those controls has already been restored. I'm wondering
    >>> if there is a way to restore the ViewState for those controls once
    >>> again.
    >>>
    >>> Thanks.
    >>> -Oleg
    Brock Allen, Jul 28, 2005
    #4
    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. Earl Teigrob
    Replies:
    2
    Views:
    358
    Earl Teigrob
    Feb 20, 2004
  2. Keith
    Replies:
    4
    Views:
    733
    DalePres
    May 2, 2004
  3. =?Utf-8?B?UGhpbA==?=

    loginview in gridview not rebinding

    =?Utf-8?B?UGhpbA==?=, May 11, 2006, in forum: ASP .Net
    Replies:
    1
    Views:
    490
  4. ssecorp
    Replies:
    5
    Views:
    319
    Terry Reedy
    Jul 12, 2008
  5. D
    Replies:
    0
    Views:
    197
Loading...

Share This Page