Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

beginer in ASP - how to implement counter

0 views
Skip to first unread message

Mario Krsnic

unread,
Jun 18, 2006, 6:21:00 AM6/18/06
to
Hello everybody,
This works in vb.net. The value of n increments:

Public Class Form1
Dim n%
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button1.Click
n = n + 1
Label1.Text = n
End Sub
End Class

The "same" thing does not work in ASP.NET (VWD 2005). The value of n remains
allways 1.

Partial Class _Default
Inherits System.Web.UI.Page
Dim n%
Protected Sub Button1_Click(ByVal sender As Object, ByVal e As
System.EventArgs) Handles Button1.Click
n = n + 1
Label1.Text = n
End Sub
End Class

What should I do to make n increase?
Thanks for every advice.
Mario


Göran Andersson

unread,
Jun 18, 2006, 6:41:03 AM6/18/06
to
The page class is instantiated every time the page loads. That means
that you get a new variable n every time the page loads.

Web pages are stateless by design. If you want to maintain a value
across pages, you have to use one of the methods for maintaining state:

http://msdn.microsoft.com/library/default.asp?url=/library/en-us/vbcon/html/vbconIntroductionToWebFormsStateManagement.asp

V

unread,
Jun 18, 2006, 7:11:38 AM6/18/06
to
You can also store n in the cache and increment and store it back to
the cache everytime. But of course, in the cache, it becomes a global
variable.

- Vaibhav

Juan T. Llibre

unread,
Jun 18, 2006, 9:08:39 AM6/18/06
to
Hi, Mario.

Why do I get the feeling that this is a
basic programming class homework assignment ?

:-)

In any case, try this :

Public Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click
Label1.Text = Val(Label1.Text) + 1
End Sub

Also, please don't refer to ASP.NET as "ASP", per your subject.

ASP is ASP; ASP.NET is ASP.NET.
Let's keep them separate so there's no confusion.

Juan T. Llibre, asp.net MVP
aspnetfaq.com : http://www.aspnetfaq.com/
asp.net faq : http://asp.net.do/faq/
foros de asp.net, en español : http://asp.net.do/foros/
===================================
"Mario Krsnic" <pdl@remove_sofort-start.de> wrote in message news:e739ef$s5a$1...@online.de...

Juan T. Llibre

unread,
Jun 18, 2006, 9:14:37 AM6/18/06
to
re:

> But of course, in the cache, it becomes a global variable.

And, of course, too, that won't do.

If all he wants to do is increment the value, he can simply use :

Label1.Text = Val(Label1.Text) + 1

If he want to use the result for something else,
he can capture it to a variable and process it.

Juan T. Llibre, asp.net MVP
aspnetfaq.com : http://www.aspnetfaq.com/
asp.net faq : http://asp.net.do/faq/
foros de asp.net, en español : http://asp.net.do/foros/
===================================

"V" <vaibhav...@gmail.com> wrote in message
news:1150629098.2...@r2g2000cwb.googlegroups.com...

Juan T. Llibre

unread,
Jun 18, 2006, 9:14:01 AM6/18/06
to
If all he wants to do is increment the value, he can simply use :

Label1.Text = Val(Label1.Text) + 1

If he want to use the result for something else,
he can capture it to a variable and process it.


Juan T. Llibre, asp.net MVP
aspnetfaq.com : http://www.aspnetfaq.com/
asp.net faq : http://asp.net.do/faq/
foros de asp.net, en español : http://asp.net.do/foros/
===================================

"Göran Andersson" <gu...@guffa.com> wrote in message news:uFHM8Osk...@TK2MSFTNGP05.phx.gbl...

0 new messages