beginer in ASP - how to implement counter

  • Thread starter Thread starter Mario Krsnic
  • Start date Start date
M

Mario Krsnic

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
 
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
 
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/
===================================
 
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/
===================================
 
Back
Top