tags:

views:

30

answers:

4

I am working on a project which creates controls dynamically for a form in the page_load event, loads in their current values from the database and saves their values (using FindControl) when the user clicks the continue button.

When I added a control statically in the .aspx page and followed their same procedure of loading the value in the page load and saving it on the button press I found that the value would not save correctly. It seems that it wouldn't save because the click event fires after the page_load, so the page_load of the post back reverted the value and the user entered value was not saved.

The strange thing is that by changing the control to be dynamically created just as all the other controls on the page and keeping the loading and saving the same it now works. Even though the page load still creates the control with the old database value.

It seems like a very fundamental asp .net feature here but i'm just unclear as to what is going on. I suspect it is to do with the timing of creation and maybe when the view state kicks in.

+1  A: 

Hi @James,

I've run into similar problems in the past (quite a few times actually), but what helped me the most is understanding the ASP.NET Page Lifecycle.

Microsoft has an article on it which describes it pretty well, but this post by Solomon Shaffer really cleared up everything.

I suggest reading them both and coming back with additional questions regarding to a particular state, when to load/save data etc..

Hope this helps.

Marko

Marko
Thanks, i'll have a look through.
James Hulse
+1 for understanding the ASP.NET Page lifecycle. It really looks like they are messing up the control lifecycle, specially when you get a situation where a dynamic control its easier to work with than a static one.
eglasius
+2  A: 

Static page controls are created just like dynamic page controls. The difference might be coming in your Page_Load. Whenever you postback all the controls are created afresh which means they are created with their initial values. This happens because after creating the controls asp.net throws away the controls/objects.

So, when the request comes, the first thing that asp.net does it to recreate the controls by looking at their definitions (in the designer files). On each postback they are created and initialized again losing their state in the process.

But after creating the controls Asp.Net loads any viewstate that is sent along with the request which makes people think that the state is always saved at the server.

What might be happening is that either the viewstate is not enabled for your control (in case they are created in designer), in which case you may try using EnableViewState property to true of the control.

Or, when you're doing a Page_Load, you're forcefully re-initializing everything. And in process losing all the control data. If you could post the logic of Page_Load, it might get clarified.

Sidharth Panwar
I would have assumed EnableViewState was enabled by default, but you may be correct. I will try again with a statically defined control and set that property explicitly.The logic is rather simple:page_load { createAndInitialiseControls()}buttonClick { Save(FindControl("control").Text)}There really isn't much else. So I believe it must be to do with the viewstate and you are probably correct that it wasn't loading for my static control.
James Hulse
Okay I played around with the EnableViewState property and it does default to true as far as I can tell so defining it didn't help. But I disabled the value loading of my static control on postback which gave me the desired results and makes sense. So when it posts back it doesn't retrieve its value from the database but instead gets it from the view state. But this doesn't surprise me at all, its why it works with the dynamic controls that really confuses me.
James Hulse
+1  A: 

Note that you may want to use Page.IsPostBack property to avoid reinitializing values on button clicks and other events.

private void Page_Load()
{
    if (!this.IsPostBack)
    {
        // Assign values to the controls.
    }
}
MainMa
Good suggestion, but because the creation and initialisation logic is in the same section within page load, on the click of the mouse the values wouldn't be saved as its a post back and the controls would not have been created. Obviously the page design isn't ideal but i'm working with what i've got :)
James Hulse
@James Hulse: I'm talking about the case where the controls are *not* created dynamically. Which means that the controls will still be here, but not reinitialized to their default values on postback.
MainMa
@MainMa: Yes, you are correct, but I avoided this to keep the current structure of the page the same. It's not my place to make large changes in logic. But yes, this would have been one solution.
James Hulse
+1  A: 

Make sure that:

  • you are not setting the value again for the static control in Page_Load. The dynamic control are probably getting around it by grabbing the ViewState and form values at a different stage in the lifecycle.
  • The dynamic controls are added After the static control. Or at least they are added in a different container. Placement in the control's collection can affect the ViewState, although it doesn't look like your scenario / since what you mention seems to be more about the values in the current post.
  • The save is happening After the Page_Load in response to the corresponding event.
eglasius
You are correct eglasius, the dynamic controls re-read from the viewstate in page load as they are added to the controls collection. Not sure who to give the answer to...
James Hulse
choose whichever answer you think helped you the most, I'm cool with just upvote and knowing that you solved it :)
eglasius