locked
How to preserve ViewState of user control nested in DetailsView RRS feed

  • Question

  • User1206402764 posted

    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!!!!

    Tuesday, February 4, 2020 12:25 PM

All replies

  • User475983607 posted

    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.  

    Tuesday, February 4, 2020 12:37 PM
  • User1206402764 posted

    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.

    Tuesday, February 4, 2020 12:48 PM
  • User475983607 posted

    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.

    Tuesday, February 4, 2020 1:15 PM
  • User665608656 posted

    Hi Onlaeg,

    Based on your question, you can refer to the following explanation.

    1. Once page is created, you get full page lifecycle
    2. You click on some control to create user control, and you get it
    3. Now you are entering value to this control, and getting postback
    4. 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.
    5. 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.

    Wednesday, February 5, 2020 10:32 AM
  • User1206402764 posted

    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). 

    DetailsView.Fields[fileld_position].Visible = false;
    DetailsView.Fields[fileld_position].Visible = true;

    My solution is using javascript to hide DetailView's fields. And it work perfectly. 

    Thursday, February 6, 2020 2:21 PM