0

So venturing in to Web applications from Windows applications and still trying to appreciate some fundamentals. The requirement is basically to have a text box, a button and a label. When clicking the button, the text box displays a pseudo-randomly generated number between 1 and 100. The label will track these numbers as comma-separated values, whilst the text box will be updated with the latest generated number each time the button is clicked.

The web page viewed in a client browser has two HTML controls and an asp web control:

<input type="text" id="txtOne" runat="server"/>
<input type="button" id="btnOne" value="Generate" runat="server" onserverclick="btnOne_OnClick"/>
<asp:Literal ID="lblOne" runat="server"></asp:Literal>

With the supporting C# class on the server:

    public partial class Contact : System.Web.UI.Page
{

    protected HtmlInputText txtOne = new HtmlInputText();
    protected HtmlInputButton btnOne = new HtmlInputButton();
    protected int randomNum;
    Random r = new Random();
    protected System.Collections.ArrayList numbers = new System.Collections.ArrayList();
    protected string text;

    protected void Page_Load(object sender, EventArgs e)
    {

    }

    protected void btnOne_OnClick(object sender, EventArgs e)
    {
        randomNum = r.Next(1, 100);
        txtOne.Value = randomNum.ToString();
        updateLabel(randomNum.ToString());
    }

    protected void updateLabel(string value)
    {
        numbers.Add(value);
        foreach (string number in numbers)
        {
            text += number + ',';
        }

        //Remove the end ','
        lblOne.Text = text.Substring(0, text.Length - 1);
    }

When the button is clicked however, only the latest number generated is output to the label. I had presumed that the ArrayList would continue to store the previous values and the iterator loop would retrieve all these in to the text variable.

When the Page object is sent back to the client, does the equivalent of a new instance of Page get sent? This appears to be the case, and if so, what approach is used to solve this?

I suppose I could use the value of the label and increment this with the new value each time (in place of the ArrayList), but I would like to solve it using the ArrayList logic or understand the reason why this isn't correct.


TEMPORARY EDIT:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI.WebControls;

namespace myProj
{
    public partial class Contact : System.Web.UI.Page
    {

        protected TextBox txtOne = new TextBox();
        protected Button btnOne = new Button();
        protected int randomNum;
        Random r = new Random();
        protected List<string> numbers = new List<string>();
        protected string text;

        protected void btnOne_OnClick(object sender, EventArgs e)
        {
            //txtOne.Text = randomNum.ToString();
            randomNum = r.Next(1, 100);
            updateLabel(randomNum.ToString());
        }

        protected void updateLabel(string value)
        {
            if (Session["numbers"] != null)
            {
                numbers = (List<string>)Session["numbers"];
            }

            numbers.Add(value);
            Session["numbers"] = numbers;

            foreach (string number in numbers)
            {
                text += number + ',';
            }

            //Remove the end ','
            lblOne.Text = text.Substring(0, text.Length - 1);
        }

    }
}
7
  • 1
    Don't use <input type="text"> and <input type="button">, use <Asp:TextBox> and <Asp:Button>. Fields (e.g. numbers in your example) do not persist between requests, you get a new instance with every request. To persist value you need to use session variabes, e.g. Session["Numbers"]. Commented Dec 18, 2014 at 14:55
  • RE controls; I did originally. I switched to HTML controls for another test and hadn't switched them back; I ought to do so. I don't think that this will influence the aforementioned problem however? Commented Dec 18, 2014 at 14:59
  • 2
    arraylist? vs 2010? Dafuq. Commented Dec 18, 2014 at 15:02
  • 1
    @Will: the polite way to say that is, "don't use ArrayList. It's been deprecated since .NET 2.0. Use List<string> instead. Commented Dec 18, 2014 at 21:18
  • I see; my continuation of Java at work. Thanks @Will for the benevelont comment, I can only admire such an effective approach to knowledge sharing. Commented Dec 18, 2014 at 23:06

1 Answer 1

2

Whenever button will be clicked page will refresh all variables will be reinitialized so arraylist will become empty so how it will retain all values. You may need to store this arraylist in Session and than read from session and loop on it.

When button will be clicked you will first read value from session and assign it to numbers and than add new generated number in it and than assign list to session again.

Session["numbers"]=numbers;

And when reading from session

numbers=(ArrayList)Session["numbers"];

Change your updatelabel method to given below and everything should work fine

protected void updateLabel(string value)
{
    if(Session["numbers"]!=null)
        numbers = (ArrayList)Session["numbers"];
    numbers.Add(value);
    Session["numbers"] = numbers;


    foreach (string number in numbers)
    {
        text += number + ',';
    }

    //Remove the end ','
    lblOne.Text = text.Substring(0, text.Length - 1);

}
Sign up to request clarification or add additional context in comments.

8 Comments

If all variables are initialised and the List<> (formerly ArrayList) becomes empty when the page is refreshed (when the button is clicked), how can the assignment 'Session["list"]=numbers' include previous randomly-generated numbers?
you will first read value from session add new number in it and than store in session again.
Thanks. But if the Session[] variable is declared inside a method (btnOne_OnClick) how can the method (Page_Load) access the values stored within it? Would it not have to be declared as an ivar? I have tried this without success.
Session is accessible everywhere.
I've still not been able to get this to operate. I have been working through your recommendations, but upon clicking the button, the page refreshes without any values in the label. I have edited my original question with a 'TEMPORARY EDIT' with the current code I am using which is unsuccessful.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.