locked
Problem with ViewState["Count"] progressing through program RRS feed

  • Question

  • User593662928 posted

    I have a web page that presents two pictures of activities in imagebuttons and with a button under each imagebutton with a description of the activity in the picture. Each of these activities are strongly related to one of the Holland Interest Codes (RIASEC). Cathy Zou helped with the presentation (that works great). Now I am looking up the code for each activity and placing that in a label. That is working also.

    When a student clicks on a imagebutton or button on left or right, I am writing an L or R in a label and accumulating those selections in another label called [Result]. I also am writing the Holland Code related to that left or right selection into another label as a comma separated list. This is a label named [Codes]. I have UserID in these and will drop in the UserID for the current user in that position.

    The problem I have is that when I am on page number 1 and make a selection, it records the Holland Code for the same side for page number 2. I guess I am having a problem understanding the sequence by which the numbering is taking place.

    <%@ Page Title="" Language="C#" MasterPageFile="~/Site.master" AutoEventWireup="true" CodeFile="Test.aspx.cs" Inherits="Members_Assessee_Test" %>
    
    <asp:Content ID="Content1" ContentPlaceHolderID="MainContent" Runat="Server">
        <p class="text-left">
            RIPA (Richards Interest Profile Assessment)</p>
        <p>
            <asp:ImageButton ID="ImageButton1" runat="server" Width="350px" Height="210px" OnClick="ImageButton1_Click" />
    &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
            <asp:ImageButton ID="ImageButton2" runat="server" Width="350px" Height="210px" OnClick="ImageButton2_Click" />
        </p>
        <p>
            <asp:Button ID="Button1" runat="server" Height="60px" OnClick="Button1_Click" Text="Button" Width="350px" />
    &nbsp; &nbsp;
            <asp:Button ID="Button2" runat="server" Text="Button" OnClick="Button2_Click" />
    &nbsp; &nbsp;
            <asp:Button ID="Button3" runat="server" Height="60px" OnClick="Button3_Click" Text="Button" Width="350px" />
        </p>
        <p>
            <asp:Label ID="CodeL" runat="server"></asp:Label>
            <asp:Label ID="CodeR" runat="server"></asp:Label>
        </p>
        <p>
            <asp:Label ID="Result" runat="server" Text="UserID"></asp:Label>
        </p>
        <p>
            <asp:Label ID="Codes" runat="server" Text="UserID"></asp:Label>
        </p>
    </asp:Content>
    

    My Code Behind is as follows:

    using System;
    using System.Collections.Generic;
    using System.IO;
    using System.Linq;
    using System.Web;
    using System.Web.UI;
    using System.Web.UI.WebControls;
    
    public partial class Members_Assessee_Test : System.Web.UI.Page
    {
        int n;
        protected void Page_Load(object sender, EventArgs e)
        {
            if (ViewState["Count"] != null)
            {
                n = Convert.ToInt32(ViewState["Count"]);
                n++;
                ViewState["Count"] = n;
            }
    
            else
            {
                n = 1;
                ViewState["Count"] = n;
            }
            ImageButton1.ImageUrl = "https://ripastore.blob.core.windows.net/ripadb/" + n + "L.jpg";
            ImageButton2.ImageUrl = "https://ripastore.blob.core.windows.net/ripadb/" + n + "R.jpg";
    
            string[] L = null;
            if (File.Exists(Server.MapPath("~/App_Data/RipaImages/" + n + "L.txt")))
            {
                L = File.ReadAllLines(Server.MapPath("~/App_Data/RipaImages/" + n + "L.txt"));
            }
            Button1.Text = "";
            foreach (string s in L)
            {
                Button1.Text = Button1.Text + s;
            }
            string[] R = null;
            if (File.Exists(Server.MapPath("~/App_Data/RipaImages/" + n + "R.txt")))
            {
                R = File.ReadAllLines(Server.MapPath("~/App_Data/RipaImages/" + n + "R.txt"));
            }
            Button3.Text = "";
            foreach (string s in R)
            {
                Button3.Text = Button3.Text + s;
            }
    
    
            string[] LT = null;
            if (File.Exists(Server.MapPath("~/App_Data/RipaImages/" + n + "LC.txt")))
            {
                LT = File.ReadAllLines(Server.MapPath("~/App_Data/RipaImages/" + n + "LC.txt"));
            }
            foreach (string s in LT)
            {
                CodeL.Text = s;
                string lc = CodeL.Text;
            }
            string[] RT = null;
            if (File.Exists(Server.MapPath("~/App_Data/RipaImages/" + n + "RC.txt")))
            {
                RT = File.ReadAllLines(Server.MapPath("~/App_Data/RipaImages/" + n + "RC.txt"));
            }
            foreach (string s in RT)
            {
                CodeR.Text = s;
                string rc = CodeR.Text;
            }
        }
    
    
    
    
        protected void Button1_Click(object sender, EventArgs e)
        {
            Result.Text = Result.Text + ",L";
            Codes.Text = Codes.Text + "," + CodeL.Text;
        }
    
        protected void Button2_Click(object sender, EventArgs e)
        {
            Result.Text = Result.Text + ",N";
            Codes.Text = Codes.Text + ",N";
        }
    
        protected void Button3_Click(object sender, EventArgs e)
        {
            Result.Text = Result.Text + ",R";
            Codes.Text = Codes.Text + "," + CodeR.Text;
        }
    
        protected void ImageButton1_Click(object sender, ImageClickEventArgs e)
        {
            Result.Text = Result.Text + ",L";
            Codes.Text = Codes.Text + "," + CodeL.Text;
        }
    
    
        protected void ImageButton2_Click(object sender, ImageClickEventArgs e)
        {
            Result.Text = Result.Text + ",R";
            Codes.Text = Codes.Text + "," + CodeR.Text;
        }
    }

    Saturday, July 15, 2017 8:14 PM

Answers

  • User-335504541 posted

    Hi RRichards,

    I think you could try to get the text of the code in your button click event again.

    For example:

            protected void Button1_Click(object sender, EventArgs e)
            {
                if (n > 1)
                {
                    n--;
                }
                string[] LT = null;
                string code="";
                if (File.Exists(Server.MapPath("images/" + n + "L.txt")))
                {
                    LT = File.ReadAllLines(Server.MapPath("images/" + n + "L - Copy.txt"));
                }
                foreach (string s in LT)
                {
                    code = s;
                }
    
                Selection.Text = Selection.Text + ",L";
                Result.Text = Result.Text + "," + code;
            }

    Best Regards,

    Billy

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, July 18, 2017 9:32 AM

All replies

  • User-335504541 posted

    Hi RRichards,

    The problem I have is that when I am on page number 1 and make a selection, it records the Holland Code for the same side for page number 2.

    The reason is that when you click the button, the Page_Load runs before the button click event, the n has been added.

    I have modified your code, please try it.

            int n;
            protected void Page_Load(object sender, EventArgs e)
            {
                if (ViewState["Count"] != null)
                {
                    n = Convert.ToInt32(ViewState["Count"]);
                    n++;
                    ViewState["Count"] = n;
                    
                }
    
                else
                {
                    n = 1;
                    ViewState["Count"] = n;
                }
                ImageButton1.ImageUrl = "https://ripastore.blob.core.windows.net/ripadb/" + n + "L.jpg";
                ImageButton2.ImageUrl = "https://ripastore.blob.core.windows.net/ripadb/" + n + "R.jpg";
                string[] L = null;
                if (File.Exists(Server.MapPath("~/App_Data/RipaImages/" + n + "L.txt")))
                {
                    L = File.ReadAllLines(Server.MapPath("~/App_Data/RipaImages/" + n + "L.txt"));
                }
                Button1.Text = "";
                foreach (string s in L)
                {
                    Button1.Text = Button1.Text + s;
                }
                string[] R = null;
                if (File.Exists(Server.MapPath("~/App_Data/RipaImages/" + n + "R.txt")))
                {
                    R = File.ReadAllLines(Server.MapPath("~/App_Data/RipaImages/" + n + "R.txt"));
                }
                Button3.Text = "";
                foreach (string s in R)
                {
                    Button3.Text = Button3.Text + s;
                }
    
                if (n != 1)
                {
                    n--;
                }
    
                string[] LT = null;
                if (File.Exists(Server.MapPath("~/App_Data/RipaImages/" + n + "LC.txt")))
                {
                    LT = File.ReadAllLines(Server.MapPath("~/App_Data/RipaImages/" + n + "LC.txt"));
                }
                foreach (string s in LT)
                {
                    CodeL.Text = s;
                    string lc = CodeL.Text;
                }
    
                string[] RT = null;
                if (File.Exists(Server.MapPath("~/App_Data/RipaImages/" + n + "RC.txt")))
                {
                    RT = File.ReadAllLines(Server.MapPath("~/App_Data/RipaImages/" + n + "RC.txt"));
                }
                foreach (string s in RT)
                {
                    CodeR.Text = s;
                    string rc = CodeR.Text;
                }
    
            }
            
            protected void Button1_Click(object sender, EventArgs e)
            {         
                Result.Text = Result.Text + ",L";
                Codes.Text = Codes.Text + "," + CodeL.Text;
            }
    
            protected void Button2_Click(object sender, EventArgs e)
            {
                Result.Text = Result.Text + ",N";
                Codes.Text = Codes.Text + ",N";
            }
    
            protected void Button3_Click(object sender, EventArgs e)
            {            
                Result.Text = Result.Text + ",R";
                Codes.Text = Codes.Text + "," + CodeR.Text;
            }
    
            protected void ImageButton1_Click(object sender, ImageClickEventArgs e)
            {
                Result.Text = Result.Text + ",L";
                Codes.Text = Codes.Text + "," + CodeL.Text;
            }
    
    
            protected void ImageButton2_Click(object sender, ImageClickEventArgs e)
            {           
                Result.Text = Result.Text + ",R";
                Codes.Text = Codes.Text + "," + CodeR.Text;
            }

    The result is:

    Best Regards

    Billy

    Monday, July 17, 2017 4:27 AM
  • User593662928 posted

    Mr. Liu,

    Thank you so very much for helping with this program. Your logic makes total sense.

    I have adjusted my code to decrement n before reading the codes for left and right as you have suggested . It is still putting the code in for the next frame. When I step through the program, it looks like it should be doing it correctly. Also, my button1, button2, and imagebutton2 are incrementing the pictures and text to the appropriate next frame, but do not write the code in the [Selection] and [Result] labels. 

    I even put an if statement in the buttons to tell it to write the material if n is greater than 0. I was hoping to trigger some reaction, but it still does not work. I know this is very simple, but sometimes simple things can be the most difficult.

    Thank you,

    Randall

    using System;
    using System.Collections.Generic;
    using System.IO;
    using System.Linq;
    using System.Web;
    using System.Web.UI;
    using System.Web.UI.WebControls;
    
    namespace Ripa239.Members.Assessee
    {
        public partial class Ripa : System.Web.UI.Page
        {
            int n;
            protected void Page_Load(object sender, EventArgs e)
            {
                if (ViewState["Count"] != null)
                {
                    n = Convert.ToInt32(ViewState["Count"]);
                    n++;
                    ViewState["Count"] = n;
                }
    
                else
                {
                    n = 1;
                    ViewState["Count"] = n;
                }
                ImageButton1.ImageUrl = "https://ripastore.blob.core.windows.net/ripadb/" + n + "L.jpg";
                ImageButton2.ImageUrl = "https://ripastore.blob.core.windows.net/ripadb/" + n + "R.jpg";
    
                string[] L = null;
                if (File.Exists(Server.MapPath("~/App_Data/RipaImages/" + n + "L.txt")))
                {
                    L = File.ReadAllLines(Server.MapPath("~/App_Data/RipaImages/" + n + "L.txt"));
                }
                Button1.Text = "";
                foreach (string s in L)
                {
                    Button1.Text = s;
                }
                string[] R = null;
                if (File.Exists(Server.MapPath("~/App_Data/RipaImages/" + n + "R.txt")))
                {
                    R = File.ReadAllLines(Server.MapPath("~/App_Data/RipaImages/" + n + "R.txt"));
                }
                Button3.Text = "";
                foreach (string s in R)
                {
                    Button3.Text = s;
                }
    
                if (n != 1)
                {
                    n--;
                }
    
    
                string[] LT = null;
                if (File.Exists(Server.MapPath("~/App_Data/RipaImages/" + n + "LC.txt")))
                {
                    LT = File.ReadAllLines(Server.MapPath("~/App_Data/RipaImages/" + n + "LC.txt"));
                }
                foreach (string s in LT)
                {
                    CodeL.Text = s;
                    ViewState["L"] = s;
                }
                string[] RT = null;
                if (File.Exists(Server.MapPath("~/App_Data/RipaImages/" + n + "RC.txt")))
                {
                    RT = File.ReadAllLines(Server.MapPath("~/App_Data/RipaImages/" + n + "RC.txt"));
                }
                foreach (string s in RT)
                {
                    CodeR.Text = s;
                    ViewState["R"] = s;
                }
            }
    
    
            protected void Button1_Click(object sender, EventArgs e)
            {
                if (n > 0)
                {
                Selection.Text = Selection.Text + ",L";
                Result.Text = Result.Text + "," + CodeL.Text;
                }
            }
    
            protected void Button2_Click(object sender, EventArgs e)
            {
                if (n > 0)
                {
                    Selection.Text = Selection.Text + ",N";
                    Result.Text = Result.Text + ",N";
                }
            }
    
            protected void Button3_Click(object sender, EventArgs e)
            {
                if (n > 0)
                {
                    Selection.Text = Selection.Text + ",R";
                    Result.Text = Result.Text + "," + CodeR.Text;
                }
            }
    
            protected void ImageButton1_Click(object sender, ImageClickEventArgs e)
            {
                if (n > 0)
                {
                    Selection.Text = Selection.Text + ",L";
                    Result.Text = Result.Text + "," + CodeL.Text;
                }
            }
    
            protected void ImageButton2_Click(object sender, ImageClickEventArgs e)
            {
                if (n > 0)
                {
                    Selection.Text = Selection.Text + ",R";
                    Result.Text = Result.Text + "," + CodeR.Text;
                }
            }
    
    
        }
    }
    

    Monday, July 17, 2017 5:05 PM
  • User-335504541 posted

    Hi RRichards,

    I think you could try to get the text of the code in your button click event again.

    For example:

            protected void Button1_Click(object sender, EventArgs e)
            {
                if (n > 1)
                {
                    n--;
                }
                string[] LT = null;
                string code="";
                if (File.Exists(Server.MapPath("images/" + n + "L.txt")))
                {
                    LT = File.ReadAllLines(Server.MapPath("images/" + n + "L - Copy.txt"));
                }
                foreach (string s in LT)
                {
                    code = s;
                }
    
                Selection.Text = Selection.Text + ",L";
                Result.Text = Result.Text + "," + code;
            }

    Best Regards,

    Billy

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, July 18, 2017 9:32 AM