Adding a row without rebinding, reloading ViewState

O

Oleg Ogurok

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
 
B

Brock Allen

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?
 
O

Oleg Ogurok

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 said:
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?




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
 
B

Brock Allen

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.




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.

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?

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
 

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,995
Messages
2,570,225
Members
46,815
Latest member
treekmostly22

Latest Threads

Top