DotDragnet
May 24, 2012, 04:33:11 AM *
Welcome, Guest. Please login or register.

Login with username, password and session length
News: follow us on twitter @dotdragnet
 
   Home   Help Search Login Register  
Pages: [1]   Go Down
  Print  
Author Topic: .NET Dynamically Created Images - How To Persist Across Postbacks?  (Read 246 times)
Dom
Hero Member
*****
Posts: 1681


Taster of pasities. Clanger of both pots AND pans.


DominicNeagle
View Profile Awards
« on: May 11, 2011, 08:38:53 AM »

tl; dr - Does anyone know how to maintain properties of dynamically generated controls across page postbacks?

Hi all,

Got an issue with a VB.NET page of mine.

Basically, when it loads, it reads an XML file and displays a list of the products listed in the XML file. Each product has a picture, a title, and a description. Each and every product is displayed on the webpage, so the more products that are added to the XML file, the more products are displayed on the webpage.

That much is done and sorted, the trouble is that the user needs to be able to add new products to the XML file. To do this, they click an "Add Product" button, which posts back the page and displays a new set of textboxes for the user to be able to enter a title, description, and location for the image.

Again, that much is working, but when the page posts back after the user clicks "Add Product", the images that have already been generated disappear.

I know that dynamically created controls don't persist across postbacks, so you have to recreate the controls in the Page_Load event each time the page posts back so that the page loads the text into the right boxes again, but for some reason it doesn't seem to load certain properties of certain controls, and the ImageUrl property of an image control seems to be one of them.

Does anyone know how to maintain properties of dynamically generated controls across page postbacks? Simply recreating a textbox on Page_Load is enough to have the page put the text it contained back in that box, but recreating an image control doesn't seem to allow the page to set the properties of that image. So whenever the user clicks "Add Product", the page reloads and the pictures disappear.

Any thoughts? It's a real pain in the bum. Cheers!
Logged

sickpuppy
Site Chimp
Administrator
Hero Member
*****
Posts: 1912



sickpuppysoftwa
View Profile WWW Awards
« Reply #1 on: May 11, 2011, 08:52:48 AM »

Nasty dirty fix but can you create a hidden field for each record and store the url in that? .NET persistence really is a pain in the bum.

I tend to use AJAX now where possible rather than rely on persistence.
Logged

Dom
Hero Member
*****
Posts: 1681


Taster of pasities. Clanger of both pots AND pans.


DominicNeagle
View Profile Awards
« Reply #2 on: May 11, 2011, 08:57:17 AM »

Yeah I did think about that, but as you say, I thought it was a rather nasty way to go about it, plus I'd need to either have a hidden control for each product (and there are an unknown number of products) or I'd have to store all of the image URLs in the one Hidden control, and have to figure out a way to separate URLs and associate each one with the right title and description.

I think what I might do is either look into AJAX as you suggested, or perhaps have a hidden panel, and when the user clicks the Add Product button, the hidden panel is revealed and allows them to add a new product. That way, the controls can be static (but hidden) and I can just add the new product to the XML file on postback, and then reload the XML data at the end so that the new product is in the list.

I shall see what happens, but I'd like to get to grips with persistence though, as I know my manager will ask me to do things like this more often.
Logged

Pages: [1]   Go Up
  Print  
 
Jump to:  

Powered by MySQL Powered by PHP Powered by SMF | SMF © 2006-2008, Simple Machines Valid XHTML 1.0! Valid CSS!