Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Preserving Viewstate When Adding Dynamic Controls

Status
Not open for further replies.

davecapone

Programmer
Oct 3, 2000
48
0
0
US
Hi,

I have an ASP.NET Page that has an asp:table. On Postback I dynamically add rows to this Table, however on subsequent postbacks I get the following error message:


Failed to load viewstate. The control tree into which viewstate is being loaded must match the control tree that was used to save viewstate during the previous request. For example, when adding controls dynamically, the controls added during a post-back must match the type and position of the controls added during the initial request.


Because of the formatting of the Table, I cannot use a Repeater or DataGrid, also because not all cells in the table are DataBound. Is there a way to preserve Viewstate when dynamically adding TableRows tot he table so subsequent PostBacks are possible?
 
Unfortunately, I don't have the answer to your problem. However, I might still have a nugget for you.

"Because of the formatting of the Table, I cannot use a Repeater or DataGrid, also because not all cells in the table are DataBound."

I don't this this can be true. The repeater, at least, can do pretty much anything because it is basic (derives from control). It's a pain to use, though, compared to the datagrid. You might look up a couple of things.

First, if you use a datagrid, make sure to read up on templatecolumns. They look something like this:

<asp:datagrid id=&quot;myGrid&quot; runat=&quot;server&quot;
AutoGenerateColumns=&quot;false&quot;
>
<columns>
<asp:templatecolumn>
<itemtemplate>
This is static text and would appear in every row.
<%# DataBinder.Eval(Container.DataItem, &quot;FieldName&quot;) %>
The above would display the contents of &quot;FieldName&quot; ie from your datasource when you databind.
<%# GetCustomMessage(<%# DataBinder.Eval(Container.DataItem, &quot;FieldName&quot;) %>) %>
The above would send the contents of &quot;FieldName&quot; to a function called GetCustomMessage. The return of that function is what would actually be displayed
</itemtemplate>
</asp:templatecolumn>
</columns>
</asp:datagrid>

As you can see, you can have static controls, datasource fields and the return values of your own functions. You can do everything with the template column but sometimes a simple boundcolumn or buttoncolumn gets the same job done better.

I just got done making a master/detail report - that's datagrids within datagrids, so they really are very flexible.

Sorry I couldn't help you directly but there's some food for thought anyway.

LATE
 
Well maybe I am missing something, but the primary reason I thought I couldn't use a DataGrid was I needed the ability to use use rowspans and columnspans which I didn't think was possible with DataGrid, but if it can be done, I'd be interested to here how.
 
Oh and also the ability to alter a row's visibility
 
Perhaps the best option would be to use a datalist. Using a dataset, a row is emitted for every row in the datasource. Inside this row you could put your own table with colspans, rowspans galore. Perhaps you could put a div/panel around this nested table and turn visibility on/off for the div/panel. You can still use all the cool databinding features of a datagrid on a datalist. You just have to worry about how each row is displayed.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top