ViewState ?

G

G-Fit

Hello group,

I am trying to do something simple (or so do I think). A web page, with a
Button, each click on it adding a row in a Table below. The result I get is
that the Table always has only one row (the one I'm trying to add), instead
of getting one *more* row after each click. I thought ViewState would keep
my Table as it was and really add the row ?

Here is my Table in the .aspx :

<asp:Table id="tbl" Runat="server" Visible="False">
<asp:TableRow>
<asp:TableHeaderCell Text="Col1" />
<asp:TableHeaderCell Text="Col2" />
<asp:TableHeaderCell Text="Col3" />
</asp:TableRow>
</asp:Table>

and here is the code behind for the button :

TableRow row = new TableRow();
TableCell cell = new TableCell();
cell.Text = "<some computed text>";
row.Cells.Add(cell);
cell = new TableCell();
cell.Text = "<some computed text>";
row.Cells.Add(cell);
cell = new TableCell();
cell.Text = "<some computed text>";
row.Cells.Add(cell);
tbl.Rows.Add(row);
tbl.Visible = true;
 
K

Kevin Yu [MSFT]

Hi G-Fit,

First of all, I would like to confirm my understanding of your issue. From
your description, I understand that you need to add rows to a Table control
each time a button is clicked. If there is any misunderstanding, please
feel free to let me know.

As far as I know, this cannot be achieved automatically by viewstate. The
newly added rows were not written to viewstate, so each time you click a
button, it will only show the row with the latest added values.

For workaround, we have to add the rows in Page_Load event handler. Each
time the button is clicked, we add the row values to an array, and save the
array in viewstate manually. When the page is loaded, we get the array from
the viewstate and add rows to the Table.

HTH.

Kevin Yu
=======
"This posting is provided "AS IS" with no warranties, and confers no
rights."
 
G

G-Fit

Kevin Yu said:
Hi G-Fit,

First of all, I would like to confirm my understanding of your issue. From
your description, I understand that you need to add rows to a Table control
each time a button is clicked. If there is any misunderstanding, please
feel free to let me know.

You got it right.
As far as I know, this cannot be achieved automatically by viewstate. The
newly added rows were not written to viewstate, so each time you click a
button, it will only show the row with the latest added values.

For workaround, we have to add the rows in Page_Load event handler. Each
time the button is clicked, we add the row values to an array, and save the
array in viewstate manually. When the page is loaded, we get the array from
the viewstate and add rows to the Table.

OK. I do not know how to manually save something in the viewstate, so
instead I saved my ArrayLists in Session variables. It works, but if saving
in the viewstate is better, I'd like to know.
 
K

Kevin Yu [MSFT]

Hi G-Fit,

Saving to ViewState is the same as saving to Session. Use
ViewState["arrayListName"] = arrayList; to save the arraylist to ViewState.
Since ViewState is put in the page, saving the object to ViewState will
consume less resource on the server than Session.

Kevin Yu
=======
"This posting is provided "AS IS" with no warranties, and confers no
rights."
 

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

Top