"Cannot load ViewState" after dynamic control changed - c#

In my ASP.NET page I have to dynamically choose and load a custom control, depending on the selected value in a dropdownlist.However I encountered the following problem:
When the parameters of the dynamically loaded control are changed, and then the selection in the dropdownlist is changed( thus forcing me to load a different dynamic control the next time the page reloads ), I end up with a "Cannot load ViewState" exception.I assume that this happens because the ViewState is trying to restore the parameters of the old control and it doesn't find it.
So , is there any way to stop the viewstate from attempting to restore the state of the non-existig control?

You should load the controls the exact same way initially and then alter then after LoadViewState or disable the viewstate on the dynamic controls you know will not be in sync with the page.

It sounds like the state of the drop down / added control is not being restored before you are restoring the view state. If you have the drop down defaulted to show control X, and the user changes it to show control Y, the page must add control Y to the control collection before view state is restored.

Had the same issue where a variable length list of controls was added, rearranged and/or modified by the user and is changable during each postback.
The answer is surprisingly simple.
When you create the dynamic control set "EnableViewState = False" before you add it to the pages control collection. Then no viewstate information is stored and regardless of how many dynamic controls are added or removed or re-ordered the viewstate for everything else will work correctly.
If your adding these dynamically you are normally setting all the properties anyways so it didn't actually create any work in my case which is very similar.

I've the same issue with grid control. I was binding dataviews dynamically and according to DarrenMB's solution I've just write EnableViewState = false; and problem solved.
Infragistics.Web.UI.DataSourceControls.DataView dvMesaj = new Infragistics.Web.UI.DataSourceControls.DataView();
whdsShowMessages.DataRelations.Clear();
whdsShowMessages.DataViews.Clear();
whgridShowMessages.Rows.Clear();
EnableViewState = false; //here is the solution..
whdsShowMessages.DataViews.Add(dvKisi);
whdsShowMessages.DataViews.Add(dvMesaj);

Related

Get all items from Repeater's datasource when using pagination

I've got a Repeater control, bound to a PagedDataSource, which datasource is a list of custom controls I've made. These custom controls contains a couple of text boxes.
I have a save button, and when it is clicked I want to save the data in all the custom controls to a database, no matter which page they are on - but currently I only got access to the custom controls displayed on the current page.
What I've tried to do is to, in the btnSave_Click event, create a new temporary datasource equal to the current one, except its not a PagedDataSource. That way my repeater contains all custom controls - BUT - the changes made in the textbox fields are no longer available. I then tried to add JavaScript onchange events on the textboxes in the custom control, so that a postback would be fired whenever text was changed, and the property in the user control codebehind would be updated. This didnt work either.
Any ideas?
save the changed values on each page index changing event (or prev /next buttons) into your persistance object (List)
http://www.dotnetfunda.com/articles/show/1611/how-to-select-multiple-records-from-multiple-pages-of-the-gridview-and
The reason your non-PagedDataSource is empty is because the changes in your text box exist in the client and not on the server - you'll need to synchronise the values from your controls with the empty slots in your repeater.
The Repeater does not have built-in Pagination (like the GridView or other complex controls) so it does not offer events such as the PageIndexChanging event. I assume therefore, that you have your own Page navigation implementation. You should therefore call the function you have presented within that implemented function.
Try Using a generic List and Skip and Take methods of that

Odd Behavior with Viewstate on Dynamically Loaded Control

I am dynamically loading user controls to switch between views in my project. I am aware that I need to reload my user control on every postback. The odd thing is my viewstate for the control is maintained even though the control is gone? I confirm that the panel I loaded it into is empty and then I check the view state and my value is there.
What's stranger is that if I load a different control, it can see the viewstate from the previous control? I checked and my page cannot see viewstate from my dynamically loaded control and visa versa. This makes me think the user control is treated as its own page. The confusing part is why the second view I load can see values from the first and why the values are there even though I the control has disappeared?
I also find this section of the code to be useless. Why is it removing the control? The panel is always empty (the code is from Telerik):
string controlId = LatestLoadedControlName.Split('.')[0];
Control previousControl = pnlControlPlaceholder.FindControl(controlId);
if (!Object.Equals(previousControl, null))
{
this.pnlControlPlaceholder.Controls.Remove(previousControl);
}
I looked at several posts and most say that viewstate is lost on every postback, although this is not the case for me. Perhaps because I'm using update panels. Although if an intial request handles an event and then reloads the same control again, the viewstate is lost. It only seems to preserve the viewstate on the very next postback.
Can anyone explain this odd behavior of sharing viewstate between user controls or why it is there even though the control is lost?
Apparently you can read viewstate between pages in two scenarios... Cross page postback and when using Server.Transfer. I believe the cross page postback scenario would explain what I am seeing.

Controls inside the dynamically added web user control does not retain there value on post back caused by the dropdown list

i am dynamically adding and removing a different user control from a to a place holders.
The user controls have lot of asp.net controls like cascading dropdowns with autopostback true.
The whole page is in a update panel.
After every postback i again add the control added previously and the which control is added previously is maintained in a view state.
The problem: Only first time the content in the textboxes get cleared after the first postback(caused by changing the dropdown)
Any help is appreciated
You need to make sure that your dynamic controls are being added in the Page_Init event in order for them to retain their data.

Using C#, how can I read the content of dynamic created textboxes?

Hy,
I have created some dynamic textboxes with standard content.
Does anyone know how can I read the content of these textboxes (assuming that user modified the standard content) when I press one button?
Thanks a lot.
Jeff
Update
This is how I am creating the textboxes:
foreach (string name in listOfNames)
{
TextBox tb = new TextBox();
tb.Text = name;
tb.BorderStyle = BorderStyle.None;
tb.BorderWidth = 0;
tb.Font.Name = "Arial";
tb.Font.Size = 8;
}
The specific will vary depending on the technology you are using. However the concept would remain very similar, though for ASP.NET it will be a little more interesting.
WinForms/WPF/Silverlight
Maintain a list of the dynamically created textboxes and when the button is pressed you can run through the list of textboxes and read the Text property to get the user input.
ASP.NET - After the tag update it seems this section is most appropriate to your requirement.
For ASP.NET you will need to create the textboxes in an override of the OnInit method, this should happen on each postback. Then in the Button.Click event you can read the user input from the textboxes that you created in the OnInit function. You need to ensure that the controls are created with the same ID on each post back.
You need to ensure that the text boxes are recreated on every postback.
If you do not recreate them, you will not be able to access their properties or events.
The best place to create dynamic controls is the page Init event handler.
I suggest reading up on the ASP.NET page life cycle.
Update (following updated question)
Make sure to set an ID (and a different one, at that) for the text boxes, so you can refer to them later on.
I can't see where you are adding these controls to the page either.
Request.Form is a collection of Key-Value pairs that represents all of the data coming back from the ASP.NET request. If you access that you can get any value whether its control is specified in the ASPX code or dynamically created in the code behind file.
You can also get their values placed back in them automatically if you recreate them during the init part of the page lifecycle. If you do this, their values will be set when ASP.NET recreates the state of the page and applies the values from the form.
You should be able to access them as you would a normal control, you just need to get a reference to the control.
Remember to re-create all controls on each postback.
this is a useful article on dynamic controls in asp.net

How to clear Viewstate?

I am having a problem in which two controls which are added programmatically are trying to load each other viewstate, I want to clear the viewstate before loading the controls, I tried Viewstate.Clear but it did nothing, when I disable viewstate on the container of my controls everything works fine except that the control's state is not kept. Is there a way to clear viewstate of just a specific control?
From your description, it would seem that you are making one of the common mistakes when loading your dynamic controls - either you are loading them too late or you are not assigning them unique IDs (and assigning them the same unique id each time a postback occurs).
If this is indeed your problem, then clearing the viewstate is not the appropriate action to be taking. It is quite simple to fix, check these three links:
http://msdn.microsoft.com/en-us/library/ms972976.aspx
https://web.archive.org/web/20210330142645/http://www.4guysfromrolla.com/articles/092904-1.aspx
http://geekswithblogs.net/shahed/archive/2008/06/26/123391.aspx
Yes ,
string controlName = "name of control";
ViewState[controlName] = null;
// create control, add it to the page
If ViewState gets in your way and you haven't done so already, please read
TRULY understanding ViewSate
It will make you much more comfortable in working with ViewState and the whole ASP.NET page lifecycle.
Assuming both controls inherit from System.Web.UI.Control. You can disable their individual ViewStates by setting their EnableViewState to false. Also you can clear their ViewState, as each control has a ViewState property.
Make sure the id's of the two programmatically added controls are different and the ViewState problem should go away.
You can disable viewstate on a specific control:
EnableViewState="False"

Categories