Does anyone know how to maintain the viewstate of a dynamically created table?
I create a table only during the first load (i.e. Not IsPostBack), but then during post back the dynamically created table just goes away...ProtectedSub Page_Load(ByVal senderAsObject,ByVal eAs System.EventArgs)HandlesMe.Load
IfNot IsPostBackThen
MakeTable()
EndIf
EndSub
Sub MakeTable()Dim iAsInteger = 1
Dim jAsInteger = 1For i = 1To 5
Dim rwAsNew TableRow
For j = 1To 3Dim rcAsNew TableCell
Dim tbAsNew TextBoxtb.Text = i * j
rc.Controls.Add(tb)
rw.Controls.Add(rc)
Next
Me.Table1.Rows.Add(rw)Next
EndSub
You have to recreate dynamically-created controls with each postback, so the easiest way to do it is to take the code out of the 'If Not IsPostBack Then' block.
bpw:
You have to recreate dynamically-created controls with each postback, so the easiest way to do it is to take the code out of the 'If Not IsPostBack Then' block.
So, does that mean that you cannot do any processing on dynamically created controls?
To elaborate... what if I want to use the text which was typed in some of the dynamically created controls... like updating fields, etc...
If I keep recreating it on the page_load, what would happen is that the user entered information will be lost... and hence cannot be used for processing...
If, for example, a user types a value into a dynamically created textbox (is this what you mean by fields?), the value will be retained by ViewState, but the control itself will not. So rebuilding the control on postback will restore the text.
All controls must be rebuilt in the same order, which means that any dynamically created controls must be added at the end of your existing dynamically created controls or ViewState will become corrupted.
It's been a while since I've done this type of work, but I've created a page that adds rows to a table and the row's cells also contained controls – a DropDownList, two textboxes and a button. The row and all the controls had to be recreated with each postback. I found it a real challenge.
A much easier solution, if there is a maximum number of rows that can be added (and the number isn't too large) is to create a table with the maximum number of rows and use the 'visible' property to 'add' and 'delete' them.
0 comments:
Post a Comment