locked
viewstate has a wrong value RRS feed

  • Question

  • User1310055179 posted

    Hi,

    I have a dynamic web-form app in asp.net and c#.

    I have multiple users that can access the application form the same client computer (one after another).

    Whenever a user selects a form from the list, he/she can edit the form or add new one to the db.

    The form code, status (new/edit/view) and id are saved in viewstate variables, like this:

    public int formStatus
           {//1=NEW, 2=UPDATE, 3=COPY, 4=VIEW
               get
               {
                   int result = 0;
                   object v = ViewState["formStatus"];
     
                   if (v != null)
                   {
                       result = (int)v;
                   }
     
                   return result;
               }
     
               set
               {
                   ViewState["formStatus"] = value;
               }
           }

    I have installed the application on my client's server but ran into a huge problem.

    The users sometimes get different values for the viewstate and I don't understatnd what is the reason for that.

    In some cases two different forms are loaded together on the same screen. I suspect it has something to do with the viewstate variables.

    I don't reset their values with NULL every time a user is logged in and multiple users can log in from the same client machine.

    Does anyone has any  idea what can be the reason?

    Monday, July 9, 2018 1:25 PM

All replies

  • User-525215917 posted

    Hi,

    ViewState doesn't seem to be the problem as ViewState is just hidden field on form. You say in the beginning of your post that application is dynamic. What it means? Do you have there dynamic loading of controls based on some conditions? If there is some mess with post-backs and dynamic loading of user controls then it's possible to get into situation where you see some unexpected controls on your form.

    Monday, July 9, 2018 1:48 PM
  • User1310055179 posted

    I thought it had someting to do with the view state because that is where I save my application code.

    The form is being rendered based on the selected value form a dropdownlist. 

    All of the form's logic is saved on a text file and since there is a lot of logic and the form is dynamic (fields data and visibility can change according to user input) I render the entier form in each postback.

    This is the function that creates the form:

    CreateDynamicForm(formStatus, app_Code, formID, focus);

    Inside the function:


    if (code == "")                 {                     if (Convert.ToString(Request.QueryString["TABLE"]) == null)                     {                         app_Code = "";//                     }                     else                     {                         app_Code = Convert.ToString(Request.QueryString["TABLE"]);                     }                 }                 else                 {                     app_Code = code;                 }
    protected override void CreateChildControls()
            {
                base.CreateChildControls();
                CreateDynamicForm(formStatus, app_Code, formID, 0);
                ChildControlsCreated = true;
            }
    
    protected void control_TextChanged(object sender, EventArgs e)
            {.....
    CreateDynamicForm(formStatus, app_code, formID, focus);}

    For each field I remove the previouse control and recreate it, For example:

    CustomTextBoxControls.TextBoxWithNumberType vTextBox = (CustomTextBoxControls.TextBoxWithNumberType)MainForm.FindControl(FldType + "_" + i);
                if (vTextBox != null)
                {
                    MainForm.Controls.Remove(vTextBox);
                }

    The user complained that in the middle of filling the form, after he changed on of the fields ( a postback was triggered), the current form was mixed with another form.

    In other words, since my function CreateDynamicForm is running twice in a postback, the app_code value was changed somehow during the postback (between the first an second runs of the function).

    This is the definition of the app_code:

    public string app_Code
            {
                get
                {
                    string result = "";
                    object v = ViewState["app_Code"];
     
                    if (v != null)
                    {
                        result = (string)v;
                    }
     
                    return result;
                }
     
                set
                {
                    ViewState["app_Code"] = value;
                }
            }

    I could see an error message in the custom error log file.  But it is not detailed enough to understand the problem.

    Exception Type: System.IndexOutOfRangeException
    Exception: Index was outside the bounds of the array.

    In my local machine I can't see any errors so I think it might occour only in case more than one user is using the app.

    Is it possible that because there is another error the app_code changes it's value to something else? where does it get its value from?

    Or maybe the error is because of the app_code being run over by another user?

    Monday, July 9, 2018 2:16 PM
  • User-525215917 posted

    Building forms dynamically is kind of risky business but not an anti-pattern - you must be very careful. 

    Controls on ASP.NET page form a hierarchy. When post-back is made then the hierarchy must be exactly the same as it was before as otherwise there will be problems with ViewState and event binding. Back in time when I updated some legacy applications written on ASP.NET Forms I spent hours finding out where control hierarchies are broken during post-backs. 

    To analyze controls hierarchy you can write recursive method that prints out controls tree to screen or to some file.

    Tuesday, July 10, 2018 4:10 AM
  • User1310055179 posted

    According to my code description, when I create the controls, do I need to enable or disable their viewstate in codebehind?

    iDropDownList.EnableViewState = false;
    
    Tuesday, July 10, 2018 7:56 AM
  • User-525215917 posted

    If you want controls to remember their values then you need viewstate

    Thursday, July 12, 2018 10:17 PM