Jump to content
Xtreme .Net Talk

Recommended Posts

Posted (edited)

Why won't 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?

 

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

 

<%@ 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>

Edited by travisowens
Experience is something you don't get until just after the moment you needed it
Posted

Since you are storing the value in your own variable, and not using the text of the label you will need to do something like

 

 

public int Count
{
	get 
	{
		return Session("m_count");
	}
	set
	{
		Session("m_count") = value;
	}
}

 

The reason for this is because everytime you click the button the page reloads, and the value is reset back to 0 and then increased.

 

Putting the value in a Session Variable will keep the value across page loads, additonally you could use this value in a completely different webpage through: Session("m_count").

~Nate�

___________________________________________

Please use the [vb]/[cs] tags on posted code.

Please post solutions you find somewhere else.

Follow me on Twitter here.

Posted

Thanks!

 

I wasn't even thinking, my brain is in WinForms mode, I don't know why I had forgotten about using a session.

 

PS: in any kind of collection/array in C# it's [] not ()

Experience is something you don't get until just after the moment you needed it
Posted
Oops, I didn't compile the code, just wrote it in the forum here. Glad you caught it, and knew the difference. Working on a VB project now, so I'm in VB mode...

~Nate�

___________________________________________

Please use the [vb]/[cs] tags on posted code.

Please post solutions you find somewhere else.

Follow me on Twitter here.

Posted
No biggie, I do it too, for awhile I was working on a classic ASP app and when I went back to my C# code, I forgot all my ;s and didn't notice until the build didn't work.
Experience is something you don't get until just after the moment you needed it

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...