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);
}
}
}
<input type="text">and<input type="button">, use<Asp:TextBox>and<Asp:Button>. Fields (e.g.numbersin 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"].ArrayList. It's been deprecated since .NET 2.0. UseList<string>instead.