Ok so I am getting into programing in ASP.Net with C#. I am trying a very simple procedure but it is very buggy. So I have the following ASP code:
<asp:Button ID="Button1" runat="server" Text="Show Numbers" onclick="Button1_Click1" />
<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
<asp:CheckBox ID="CheckBox1" runat="server"
oncheckedchanged="CheckBox1_CheckedChanged" />
I then have the following C# code behind it:
int i = 0;
List<int> Chosen = new List<int>();
public void Page_Load(object sender, EventArgs e)
{
}
public void Button1_Click1(object sender, EventArgs e)
{
if (i == 0)
{
TextBox1.Text = "Nothing here!";
}
else if (i == 1)
{
TextBox1.Text = Chosen[0].ToString();
}
}
protected void CheckBox1_CheckedChanged(object sender, EventArgs e)
{
if (CheckBox1.Checked == true)
{
Chosen.Add(1);
i++;
CheckBox1.Checked = true;
}
else if (CheckBox1.Checked == false)
{
Chosen.Remove(1);
i--;
CheckBox1.Checked = false;
}
}
The goal of the code is to have a checkbox on the screen. If it is checked I want to add the number 1 to my list (Chosen) also, when I push the button I want the textbox to display the number 1. If the checkbox becomes unchecked I want the number to get removed from the list and when I push the button I want it to display "Nothing here!".
The problem is, sometimes it works and sometimes it doesn't. For example if I click the box then the button it works. Then when I click the button again it says "Nothing Here!" it should stay as a 1.