NEWBIE - How to survive the postback.

  • Thread starter Thread starter Jim Mitchell
  • Start date Start date
J

Jim Mitchell

I load a table and a textbox on the initial pageload. The value in the
textbox survives the postback, the values in the table do not.

Is there a way for the values in the table control to survive a postback?

Thanks in advance.
 
Hi,

The most likely reason is that the control you use to render the table
doesn't save its contents to the ViewState. So, you three options:

1. Use an alternative control that is ViewState-aware (I don't do much
WebForms programming now so I am not 100% sure one exists)
2. Modify the control used to render the table to save/restore contents to
the ViewState
3. Re-design the code responsible for rendering the table to save/restore
the loaded data.

Unfortunately, there are little details about your page to suggest something
more specific.
I would also say that if you use server-side controls to render table cells'
contents, they should automatically use ViewState unless you explicitly
disabled ViewState usage. But if you use plain Response.Write, you will most
likely have to store/retrieve the data manually.
 
Thanks for trying. I somehow dropped the sample page. If I add an
imagebutton to a table at run time it does not survive the postback. At
design time there is no problem. I guess my question was, how can I make an
imagebutton added at design time survive the post back. See code below.

The only way seems to be to make the image again on every postback. But
this seems like a lot of overhead. You should be able to create an image
with an event handler and trap the click event without having to recreate
the image on every post back.

Private Sub Page_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load

'Put user code to initialize the page here

If Not IsPostBack Then

MakeImage()

End If

End Sub

Sub MakeImage()

Dim tr As System.Web.UI.WebControls.TableRow

Dim td As System.Web.UI.WebControls.TableCell

imgNote = New ImageButton

imgNote.ImageUrl = "images/small_note.gif"

AddHandler imgNote.Click, AddressOf GoEditNote

td = New TableCell

td.Controls.Add(imgNote)

tr = New TableRow

tr.Cells.Add(td)

Table1.Rows.Add(tr)

End Sub

Public Sub GoEditNote(ByVal sender As System.Object, ByVal e As
System.Web.UI.ImageClickEventArgs) Handles imgNote.Click

MakeImage()

Response.Write("Image Button was Clicked")



End Sub





Dmitriy Lapshin said:
Hi,

The most likely reason is that the control you use to render the table
doesn't save its contents to the ViewState. So, you three options:

1. Use an alternative control that is ViewState-aware (I don't do much
WebForms programming now so I am not 100% sure one exists)
2. Modify the control used to render the table to save/restore contents to
the ViewState
3. Re-design the code responsible for rendering the table to save/restore
the loaded data.

Unfortunately, there are little details about your page to suggest something
more specific.
I would also say that if you use server-side controls to render table cells'
contents, they should automatically use ViewState unless you explicitly
disabled ViewState usage. But if you use plain Response.Write, you will most
likely have to store/retrieve the data manually.

--
Dmitriy Lapshin [C# / .NET MVP]
X-Unity Test Studio
http://x-unity.miik.com.ua/teststudio.aspx
Bring the power of unit testing to VS .NET IDE

Jim Mitchell said:
I load a table and a textbox on the initial pageload. The value in the
textbox survives the postback, the values in the table do not.

Is there a way for the values in the table control to survive a postback?

Thanks in advance.
 
Hi Jim,
design time there is no problem. I guess my question was, how can I make an
imagebutton added at design time survive the post back. See code below.

Any control added at design time (I mean added with the Web Forms designer
or manually by editing the aspx file contents) should survive any postbacks
by definition. But if you create your control within the Page_Load event
handler, it won't survive a postback - just because ASP .NET disposes the
page class instance as soon as the request has been processed. And while
"static" content can always be re-created from the .ASPX file, controls
created from code should be re-created every time the page gets loaded.

Anyhow, there is not so much overhead as you might think. ASP .NET actually
creates server controls for each < ...RUNAT="Server"> tag behind the scenes
so if you would have added the imagebutton in the Web Forms designer, the
corresponding control would be created for you.
 
Back
Top