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 wOOdy-Soft on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

My (WebControl) Table disappears on PostBack! 1

Status
Not open for further replies.

DotNetter

Programmer
May 19, 2005
194
US
I have a ASP.NET (WebControl) table that I created on the Designer. It seems like it's losing state, though.

In PageLoad I have the following:
Code:
If Me.IsPostBack = False Then
					tblVendors = CType(Session("tblVendors"), System.Web.UI.WebControls.Table)
					Session.Add("vendorCounter", 1)
					AddAVendor()
				End If
which calls this:
Code:
	Private Sub AddAVendor()
		Dim newRow As New TableRow
		Dim cellVendor As New TableCell
		Dim cellFilename As New TableCell
		Dim cmbVendor As New DropDownList
		Dim tbFilename As New TextBox
		cellVendor.Controls.Add(cmbVendor)
		cellVendor.ID = "cellVendor" & Session("vendorCounter").ToString - 1
		cellFilename.ID = "cellFilename" & Session("vendorCounter").ToString - 1
		newRow.ID = "row" & Session("vendorCounter").ToString - 1
		newRow.Cells.Add(cellVendor)
		newRow.Cells.Add(cellFilename)

		cellFilename.Controls.Add(tbFilename)

		tblVendors.Rows.Add(newRow)

		PopulateVendorsDropDown(cmbVendor)
		Session("vendorCounter") += 1
		Session("tblVendors") = tblVendors
	End Sub
That "AddAVendor" sub is also called by a button click.

It should be adding a row to the table with the first page load and then also each time the button is clicked. It is adding the first row on the first page load - that's the good news. But when the user clicks the (ImageButton) and the page posts back, there no table to be seen on the page anymore. When I check it in the Command Window, it claims it's there with the right number of rows! All parts of the table (rows, cells, table) are visibile = true.

Any ideas why my table disappears - please???

Thanks,
Dot
 
Remove the

Code:
If Me.IsPostBack = False Then

and it should be fine.

Smeat
 
I removed that line and it's acting exactly the same. The table is just completely gone when I click my ImageButton!...

Help?!?!

Thanks,
Dot
 
So you're adding the ROW on PageLoad, and on a button's click event. That should be ok, as each code-path will create the row.

But, at what point is the table created? Remember, EVERY server control completely disappears each time your code is done running. ASP.NET recreates the controls each time. It does this automatically for controls you created at design time.

If you create your own controls, dynamically, at run time, then you must re-create those controls each and every time, as well.



Thomas D. Greer

Providing PostScript & PDF
Training, Development & Consulting
 
I used the designer to declare (create) the table. It's as if the table "forgets" its rows with every postback!

I tried this simple test code, by creating "tblTest" with the designer:
Dim oRow As New TableRow
Dim oCell As New TableCell
oCell.Text = "I'm a cell"
oRow.Cells.Add(oCell)
tblTest.Rows.Add(oRow)

At postback, the row disappeared!

What's going on here?!

Thanks so much,
Dot
 
Are you caching the page at all?

Rhys
"There are some oddities in the perspective with which we see the world. The fact that we live at the bottom of a deep gravity well, on the surface of a gas-covered planet going around a nuclear fireball 90 million miles away and think this to be normal is obviously some indication of how skewed our perspective tends to be"
DOUGLAS AD
 
Rhys -

Are you asking why am I pushing the table into the session? Well, it didn't help - but I'm trying to compensate for the fact that the table "forgets" all of its rows.

I've been all over Google - I see others have this problem - but I just don't get the answer!

Thanks,
Dot
 
I was wondering (without thinking too hard as it's getting late in my working day) if the page displayed is from the client cache and that's why it's not displaying any rows - because the first instance of the page received by the client doesn't have any.

Rhys
"There are some oddities in the perspective with which we see the world. The fact that we live at the bottom of a deep gravity well, on the surface of a gas-covered planet going around a nuclear fireball 90 million miles away and think this to be normal is obviously some indication of how skewed our perspective tends to be"
DOUGLAS AD
 
And if it is, how do I get around that?

Alternatively, do I need to "force" the table into the Viewstate somehow?

Thanks,
Dot
 
Here's what I did. I modified some values and variable names cuz I didn't have them and this still needs some tweeking. The main change is I add the table to a placeholder in the addvendor sub.


Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
If Not IsPostBack Then
AddAVendor()
Else
Table1 = CType(Session("tblVendors"), System.Web.UI.WebControls.Table)
End If
End Sub

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
AddAVendor()
End Sub
Private Sub AddAVendor()
Dim newRow As New TableRow
Dim cellVendor As New TableCell
Dim cellFilename As New TableCell
Dim cmbVendor As New DropDownList
Dim tbFilename As New TextBox
cellVendor.Controls.Add(cmbVendor)
newRow.Cells.Add(cellVendor)
newRow.Cells.Add(cellFilename)

cellFilename.Controls.Add(tbFilename)

Table1.Rows.Add(newRow)

'Add the table to a place holder
PlaceHolder1.Controls.Add(Table1)

Session("vendorCounter") += 1
Session("tblVendors") = Table1
End Sub
 
If you create one row in code-behind, then you have to create that same row EVERY TIME. If you've created a total of 5 rows, through some process, then you'll need a process to re-create those 5 rows EVERY TIME.

There is nothing unusual about this. That's how ASP.NET works. That's how the web works. You must methodically recreate everything you've ever created. In between "postbacks", everything is forgotten, nothing exists.

There is no reason to expect that a complete table, with all rows, will magically reappear each time you run your server-side code. You have to persist those values somewhere. A database, for example. Then, each pass through your code, you will have to COMPLETELY REBUILD the table, from SCRATCH, and then and only then, add a new row.



Thomas D. Greer

Providing PostScript & PDF
Training, Development & Consulting
 
tgreer, you are right about the persistance and rebuilding of tables everytime. Putting the table into session takes care of the persistance. I think without the placeholder, the steps of rendering of the page happened in such way that the table didn't show up, even though it had all the rows.
 
Stsuing, your recounting of the events is accurate: the table didn't show up, although it had all the rows.

Any ideas why?

Thanks so much,
Dot
 
Ah, the dreaded Page Life Cycle. If you're working with dynamically created controls, you have to be aware of the specific steps ASP.NET goes through on PostBack.

You might think that the "Load" event is the first thing that happens when you PostBack. It isn't, it's the 4th step in the process.

It's preceded by

1. Initialization
2. LoadViewState
3. LoadPostBackData

If you want dynamically created controls to "be there" by the time you hit the Load stage, you need to create them PRIOR TO that, by overloading one of the previous stages.

The reason that adding the table to a static PlaceHolder works, is because the PlaceHolder will already be there, properly initialized and loaded, and the Add mechanism will synchronize the dynamic control (which in this case is your table with its rows).

More information on working on the Page Life Cycle:

ASP.NET Page Life Cycle and Dynamic Controls[link]



Thomas D. Greer
[URL unfurl="true"]http://www.tgreer.com


Providing PostScript & PDF
Training, Development & Consulting
 
...now my comboboxes within my table are losing the user selection on postback!

How do I get around this one?

Thanks (again)!
Dot
 
Personally I would change your whole approach and use a DataGrid or DataList. A much more standard method.
 
Agreed - for the future. Any ideas for now (quick-and-dirty :) ?

Dot
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top