Click here to Skip to main content
15,893,337 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi.

I have a 2 tier control. The Website hosts the public facing .ascx which inherits it's server side .cs (which inherits UserControl).

All of my data access is handled by my server side control, and all of the controls are populated by my public facing control.

This control handles the users roles on the website. The user can access this page via the url [host]/Roles and select a user from the dropdown. They can also access it via the url [host]/Roles/[username] which will automatically select the user from the dropdown.

When the page is loaded with a username, or when a username is selected from the dropdown, the server side control saves the username to the viewstate for later reference.

When the username is selected from the dropdown then all works ok.

When the username is selected from the url then it does not maintain the viewstate

I'm sure this has to do with my two tier control, but I don't know how to fix it.

I tested adding "test":"test" to the viewstate if !IsPostback on the public facing side and that item is maintained.

What am I doing wrong?

Username Property:
C#
...
        //Server side control
        protected string Username
        {
            get
            {
                return ViewState.GetStateItem<string>("Username");
            }
            set
            {
                ViewState.SetStateItem("Username", value);
            }
        }
...
// Extention:
        public static T GetStateItem<T>(this StateBag viewState, string key)
        {
            object obj = viewState[key];

            if (obj == null)
                return default(T);

            if (obj.GetType().IsAssignableFrom(typeof(T)))
                return (T)obj;
            return default(T);
        }
        public static void  SetStateItem<T>(this StateBag viewState, string key, T value)
        {
            
            viewState[key] = value;
        }
Posted

1 solution

Ah - I was adding to the viewstate in the OnInit on first load, then after the event handlers subsequently.

Viewstate tracking is turned on during InitComplete after OnInit.

I have moved the code to the OnLoad and it works fine

just doh!
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900