views:

243

answers:

4

I am new to asp.net. I am creating a ASP.net website using VB.net. So here's my problem

Dim myCounter as Integer = 0

Protected Sub Button1_Click(ByVal sender As Object, ByVal e As System.Web.UI.ImageClickEventArgs) Handles Button1.Click

        myCounter =  myCounter + 1

        Label1.Text = myCounter.ToString()

end Sub

As expected I always get Label Text as 0 each time click the button. How to I create global variable and increment it.

A: 

your page class gets recreated on each request... so myCounter won't exist the next time.

you can either

  • make myCounter static (not a great idea)
  • put it in the Application, Session, or Cache collection

depends on what you're trying to do

Nick Franceschina
can you give the code for putting it in Session collection and incrementing it on each button click
Peter
Don't use `static`! That will cause nothing but misery in an ASP.NET application, even for people who aren't brand new to it. Definitely for people who are.
Rex M
+3  A: 

Every time the page posts back, it is essentially starting over from scratch - anything initialized to 0, for example, will be zero again. This is because the server doesn't know anything about the last time the page ran - all it knows is you clicked a button which submits a form to that page, so it creates another instance of the page and starts again.

If you need to persist a value across postbacks, the standard method is to use ViewState:

Public Property MyCounter() As Integer
    Get
        Dim val As Object = ViewState("MyCounter")
        Return If(val IsNot Nothing, CInt(val), 0)
    End Get
    Set(ByVal value As Integer)
        ViewState("MyCounter") = value
    End Set
End Property

It's also possible to use Session, which will persist the value across all pages and requests for the life of the user's session. For that to work, you can use the same sample above, replacing ViewState with Session.

Rex M
A: 

@Rex M's suggestion for using Viewstate is good.

If the counter is not sensitive information or something you're worried about someone tampering with., here's an easier idea:

You can also use an <asp:HiddenField> and store the value there. Then it will persist between postbacks and you can increment it each time..

David Stratton
I'm curious - why do you find hidden fields easier than viewstate?
Rex M
Just because the syntax for using them is similar to setting the value in textboxes and labels. Technically, using viewstate is really just as easy, but for a beginner, we don't know if @Peter understands viewstate yet. Then again, even if he doesn't understand viewstate, this is a good way to get him to learn. It's probably a good idea to steer him in this direction purely in the interest of helping him become more well-rounded.
David Stratton
+1  A: 

Her's another method that doesn't use hidden field, viewstate, session or cache

Probably not something very 'safe' but probably saves you some time.

Assuming initial Label1.Text = 0

Protected Sub Button1_Click(ByVal sender As Object, ByVal e As System.Web.UI.ImageClickEventArgs) Handles Button1.Click

        Label1.Text = (Integer.Parse(Label1.Text) + 1).ToString()

end Sub
o.k.w
This method uses view state..
Oliver Hanappi
@Oliver Hanapp1 - Yeah, this uses the viewstate, as does my answer, but not in a way that's obviously using the view state. When someone says to me they're using the viewstate, to me that implies they're using Viewstate.Add(). Technically you're right,of course.
David Stratton
Yea, it's using viewstate indirectly, which is not the implicit usage.
o.k.w