beginer in ASP - how to implement counter

Discussion in 'ASP .Net' started by Mario Krsnic, Jun 18, 2006.

  1. Mario Krsnic

    Mario Krsnic Guest

    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
    Mario Krsnic, Jun 18, 2006
    #1
    1. Advertising

  2. 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/d...bconIntroductionToWebFormsStateManagement.asp

    Mario Krsnic wrote:
    > 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
    >
    >
    =?ISO-8859-1?Q?G=F6ran_Andersson?=, Jun 18, 2006
    #2
    1. Advertising

  3. Mario Krsnic

    V Guest

    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

    Mario Krsnic wrote:
    > 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
    V, Jun 18, 2006
    #3
  4. 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$...
    > 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
    >
    Juan T. Llibre, Jun 18, 2006
    #4
  5. 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" <> wrote in message news:...
    > 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/d...bconIntroductionToWebFormsStateManagement.asp
    >
    > Mario Krsnic wrote:
    >> 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
    Juan T. Llibre, Jun 18, 2006
    #5
  6. 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" <> wrote in message
    news:...
    > 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
    >
    > Mario Krsnic wrote:
    >> 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

    >
    Juan T. Llibre, Jun 18, 2006
    #6
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Tom Vande Stouwe MCSD.net

    Re: How to implement counter?

    Tom Vande Stouwe MCSD.net, Jul 24, 2003, in forum: ASP .Net
    Replies:
    0
    Views:
    394
    Tom Vande Stouwe MCSD.net
    Jul 24, 2003
  2. Tu-Thach

    How to implement counter?

    Tu-Thach, Jul 24, 2003, in forum: ASP .Net
    Replies:
    0
    Views:
    373
    Tu-Thach
    Jul 24, 2003
  3. The Eeediot
    Replies:
    3
    Views:
    2,228
    =?Utf-8?B?UnVsaW4gSG9uZw==?=
    Dec 22, 2004
  4. George2
    Replies:
    1
    Views:
    795
    Alf P. Steinbach
    Jan 31, 2008
  5. jcafaro10

    How to implement a counter

    jcafaro10, Sep 24, 2009, in forum: VHDL
    Replies:
    0
    Views:
    974
    jcafaro10
    Sep 24, 2009
Loading...

Share This Page