ViewState persists across POST backs - not GETs. It does not matter if the ViewState is in a User Control. I recommend setting a break point and running your code through the Visual Studio debugger.
I think so. So I set a break point in UserControl PagePreRender to watch ViewState value. But it was null.
I don't know why. I am not sure, but It seem that when I show or hide DetailView's rows, after PostBack DetailView was renew or something like that. And my UserControl was null.
Why I believe that? With button in page PostBack, UserControl's ViewState value was preserved.
Again, ViewState is deterministic. Since your code is setting ViewState in a class, ViewState will persist across Post Back. If ViewState is null after setting ViewState then there is a bug in your code.
If you need community debugging assistance then share enough code to reproduce this issue. Otherwise, the community has no way to provide help.
Based on your question, you can refer to the following explanation.
Once page is created, you get full page lifecycle
You click on some control to create user control, and you get it
Now you are entering value to this control, and getting postback
On server side postback is handled, but as you can see viewstate actions appear as soon as page is loaded.
One of main purposes of viewstate is handling control events, to see if they are changed, or save their states or something else.
If on the moment, when viewstate is loaded you control is still not constructed, then all it's events and values would be ignored.
Solution either make it static control and just hide it, either create it before viewstate actions started.
Best Regards,
YongQing.
MSDN Community Support
Please remember to click "Mark as Answer" the responses that resolved your issue.
If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.
None
0 Points
5 Posts
How to preserve ViewState of user control nested in DetailsView
Feb 04, 2020 12:25 PM|Onlaeg|LINK
Hi, I need some help.
In my aspx page, I used a DetailsView to bind data. There is a DropDownList in it and an UserControl.
Dropdownlist have a SelectedIndexChange postback to show and hide some rows of DetailsView.
In my UserControl I save a list value in ViewState.
this to preserve the value of boolean AddingNew between postbacks and it worked fine..
protected List<int> ListValue
{
get
{
return ViewState["list"] as List<int>;
}
set
{
ViewState["list"] = value;
}
}
My problem is after SelectedIndexChange postback event, ViewState of my UserControl is null. Can we preserve ViewState?
Thank for any help!!!!
All-Star
53711 Points
24042 Posts
Re: How to preserve ViewState of user control nested in DetailsView
Feb 04, 2020 12:37 PM|mgebhard|LINK
ViewState persists across POST backs - not GETs. It does not matter if the ViewState is in a User Control. I recommend setting a break point and running your code through the Visual Studio debugger.
None
0 Points
5 Posts
Re: How to preserve ViewState of user control nested in DetailsView
Feb 04, 2020 12:48 PM|Onlaeg|LINK
I think so. So I set a break point in UserControl PagePreRender to watch ViewState value. But it was null.
I don't know why. I am not sure, but It seem that when I show or hide DetailView's rows, after PostBack DetailView was renew or something like that. And my UserControl was null.
Why I believe that? With button in page PostBack, UserControl's ViewState value was preserved.
All-Star
53711 Points
24042 Posts
Re: How to preserve ViewState of user control nested in DetailsView
Feb 04, 2020 01:15 PM|mgebhard|LINK
Again, ViewState is deterministic. Since your code is setting ViewState in a class, ViewState will persist across Post Back. If ViewState is null after setting ViewState then there is a bug in your code.
If you need community debugging assistance then share enough code to reproduce this issue. Otherwise, the community has no way to provide help.
Contributor
3720 Points
1043 Posts
Re: How to preserve ViewState of user control nested in DetailsView
Feb 05, 2020 10:32 AM|Yongqing Yu|LINK
Hi Onlaeg,
Based on your question, you can refer to the following explanation.
One of main purposes of viewstate is handling control events, to see if they are changed, or save their states or something else.
Solution either make it static control and just hide it, either create it before viewstate actions started.
Best Regards,
YongQing.
Please remember to click "Mark as Answer" the responses that resolved your issue.
If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.
None
0 Points
5 Posts
Re: How to preserve ViewState of user control nested in DetailsView
Feb 06, 2020 02:21 PM|Onlaeg|LINK
I found my problem.
I used following code to hide and show some field inside my DetailsView. This make DetailView be renew (or seem like to be).
My solution is using javascript to hide DetailView's fields. And it work perfectly.