travisowens
Well-known member
Why wont this int keep incrementing?
I just created a simple web page in VS2005 and dropped a button and a label. I simply want the label to increment every time the button is pushed, which works fine the first click, but after that Count is always 1.
What simple thing am I not seeing?
and if you want to see the generated HTML
I just created a simple web page in VS2005 and dropped a button and a label. I simply want the label to increment every time the button is pushed, which works fine the first click, but after that Count is always 1.
What simple thing am I not seeing?
Code:
public partial class _Default : System.Web.UI.Page
{
public int _count = new int();
public void Page_Load(object sender, EventArgs e)
{
}
public void btnIncrement_Click(object sender, EventArgs e)
{
Count = Count + 1;
lblCount.Text = Count.ToString();
}
public int Count
{
get
{
return _count;
}
set
{
_count = value;
}
}
}
and if you want to see the generated HTML
Code:
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>Untitled Page</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:Button ID="btnIncrement" runat="server" OnClick="btnIncrement_Click" Text="Increment" /><br />
<asp:Label ID="lblCount" runat="server" Text="0"></asp:Label></div>
</form>
</body>
</html>
Last edited by a moderator: