How To maintain ViewState of PlaceHolder in asp.net - asp.net

please tell me how to maintain state of placeholder. i have a placeholder in which i add many image controls dynamically but when my page get refresh all controls from placeholder gets removed from it. the enableViewstate of placeholder is set to true.. please tell me how to maintain its state..

ViewState registration happens just after the Init events in the Page lifecycle.
If you are adding your dynamic images after Init, then they are not registered as part of ViewState. Your issue will be solved if you add them to your Placeholder control during Page_Init.

I think you need to add the controls in the Page_PreInt() event

You must add dynamic controls on every page load. Controls are not stored in ViewState, only control STATES and data for controls which do not post their data.

Related

Dynamic Controls and Postback

I have a Panel. Now in that panel, i'm adding controls. It is getting added and displayed.The problem is when the page is posted back.I know the controls have to be binded again in the panel. But lets say if the user has entered some value in the dynamic created text box. Its is getting lost..
I have done this before by storing the data from the controls in session.
Every time you dynamically add a control, store the current data entered into the controls in session or viewstate for example, and then rebind on postback. Not the most elegant solution but it worked. I take it this is a webforms question?
Add dynamic control click
Save current form data
On page_load load the data from viewstate into the controls
Make sure that the controls get the same ID each time (for ex. by specifying an ID explicitly)
and you should add the dynamic controls on Page.Init so they can participate in the page's life-cycle.
There's an article on 4guysfromrolla.com, Dynamic Web Controls, Postbacks, and View State

UserControl, PlaceHolder and UpdatePanel On PostBack

I dynamically load a User Control, with an Update Panel inside a Place Holder.
When I click a button from the User Control, should refresh the Update Panel contents, but it refresh the entire page instead, and the User Control is disappearing from the page, because the page's Page_Load does not load anything if it's a PostBack.
How I can fix it?
Whenever a partial or full postback happen , Automatically all update() method of all updatepanels fire . For preventing such this behavior you need to set UpdateMode="Conditional" property . In this situation you need to specify asynchronous trigger Or ChildrenAsTriggers=true .
for preventing a dynamically loaded-usercontrol to be disappear ,It's good to save it in ViewState , Here is a tutorial and sample application
I think you'll need to reinject the control in page_load or pre_render. Dynamically created controls don't live through postback.
Make sure you are creating the control EVERY page request, regardless of GET/POST. Also, make sure you are giving it the same ID.
I like to override the CreateChildControls method.
You need to add the control page to the page in the page_init method. It has to be added on each post back. The control will retain all the values even after adding it back.
There is a full working example at this link.

control values on refresh

I have set an asp.net page to refresh every after sometime. The problem is that once the page is refresh, the state of the controls are lost, like the dropdown loses all its item. I checked the viewstate property of the controls but they are set to be enabled. Any idea why is it so?
Thanks,
Because a web form is also an HTML form. You need to POST the form instead of refreshing.

Does Page class adds ViewState for Controls created in CreateChildControls in ASP.NET

Does Page class adds ViewState for Controls created in CreateChildControls, I mean I have a page which I add some controls to the page and all of them are Literal and EnableViewState is false, my question is are there any ViewState entry gor them in Page's ViewState?
Thank you.
You might want to read this and see if this is what blowing up you page:
http://msdn.microsoft.com/en-us/library/1whwt1k7.aspx
Looks like you can disable view state but your controls can have there own ControlState you might need to worry about.
Also if you are adding these controls into a DataGrid, The DataGrid, stores all of its contents in the view state.
Look here : http://msdn.microsoft.com/en-us/library/ms972976.aspx#viewstate_topic9

postback not raised problem

I have next situation:
I load dynamic controls during on init, and I do correct initialization.
I add dynamic control before postback
I don't add anything later in load
control is loaded and diplayed correctly
I press postback and nothing happens
Why I really don't know.. I tried everything. So control IS properly initialised. __EVENTTARGET shows the same path as the UniqueId of linkbutton that is firing it. All controls in tree have viewstate=true. So, I really don't know what this is not working.
Any idea? I am desperate.. I don't know.. if anyone could suggest me, if not solution, then just things I should check would be very good.
Is this problem just for this page or do you have other pages on the same site with the same problem?
I am assuming that you have the same problem on all pages.
It could be relate do javascript not being allowed. You could try to add the site to local intranet security are, then refresh the page.
Dynamic controls have to be added back to the control tree on each postback for the events to fire.
Dynamically created controls are not part of their container's viewstate, so setting it to TRUE wouldn't have any effect on the situation and are not evaluated until after the on_init call completes anyways.
I would wrap the logic that is populating these dynamic controls in with a conditional check for a postback if(!IsPostBack)
{ //Insert logic here }
If your dynamic controls take input from the user, or need access to their view state, then you would need to move this call to the Page_Load method as this is the point in the page's lifecycle where viewstate is first evaluated.

Resources