VB .Net WebProject Variables

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

I am new to VB.Net WebProjects but have written VB.Net Projects for sometime. I am having problems with variables in my WebProjets. I dim them and I can use them but they do not keep there value. I have a timer that runs and each time the time if fired that value in the variable is back to what it was the time before, even though I have added 1 to it. Does any one have any idea why this is?

Thanks in advance.

Dale
 
Where are you dimming this variable ?, dont forget once the page has been
rendered, the page object is unloaded on the server

Consider using the Session object to store this information in.

Session.Add(Key,Value)


Having said all that, the right place to ask these questions in on ASPNET
newsgroup.

microsoft.public.dotnet.framework.aspnet



Regards - OHM ( Terry Burns )




dkalsow said:
I am new to VB.Net WebProjects but have written VB.Net Projects for
sometime. I am having problems with variables in my WebProjets. I dim them
and I can use them but they do not keep there value. I have a timer that
runs and each time the time if fired that value in the variable is back to
what it was the time before, even though I have added 1 to it. Does any one
have any idea why this is?
 
Having said all that, the right place to ask these questions in on ASPNET
newsgroup.

microsoft.public.dotnet.framework.aspnet

Why, has this to do installing the framework on the ASPNET server or has it
to do with using the VB.language in a webform application?

Cor
 
Well, unless I have mistaken this, the OP is designing an ASP.NET
application. ServerControls reside on the IIS server, so the Framework has
to already be installed there.

Perhaps I just dont understand your question Cor.

Regards ( OHM - Terry Burns )
 
Hi Terry,

It was in past often that when it was a little bit web, that the sentence
came in this newsgroup to have a look at aspnet. It can be because in the
German newsgroups there is as well an ASPNET entwickler (develloper)
newsgroup.

In my opinion is there not much difference when you are develloping for a
Windowservice, a windowform, a webform, a webservice, a windowclass or
whatever using Visual Studio Net.

For a Webform is the special thing as you stated in your answer, however for
the rest is beside using special controls almost nothing what is not the
same.

And therefore did you give in my opinion the right VB language answer,
although for this because it is so small was the alternative maybe the
viewstate maybe as well posible.

Therefore I think that when it has to do with the VB language this is the
right newsgroup, and now with your renewed expirience even more.

:-)

Cor
 
Hi Cor,
Using ViewState will not work in this scenario, the reason
being that the Post will reset the ViewState even if its being changed with
a TimerClick on the server side. However, Session variables can be asjusted
by the timer.

See the following code to demonstrate. ( Assumes a Submit button present )
Note that the following code is using viewstate ( as you suggested ). Run it
this way first and see the results. Then comment out the ViewState lines and
use the session Key/Value Pairs in the Session object, and try submitting
again. You will see the counter is counting on the server while time
elapses. Submiting button will produces variable and increasing numbers
depening on the time lag.

Private Sub Page_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load
'Put user code to initialize the page here
Timer1.Interval = 1000
Timer1.Start()

If Page.IsPostBack Then
'Response.Write(Session.Item("C"))
Response.Write(ViewState("C"))
Else
'Session.Add("C", 1)
ViewState.Add("C", 1)
End If

End Sub

Private Sub Timer1_Elapsed(ByVal sender As System.Object, ByVal e As
System.Timers.ElapsedEventArgs) Handles Timer1.Elapsed
'Session.Item("C") += 1
Viewstate("C") += 1
End Sub
End Class


Hope that makes it clear.

Regards - OHM ( Terry Burns )
 
Hi Terry,

I changed your program, and that shows it even more, was for me it was
amazing. The system timer goes on and set the referenced object in the
session (and that can of course not in the viewstate).

I did not really look what was the problem when I wrote the first message,
however I saw something pointing on stateless and the session.

Thanks for letting me know, I did not know this.

However, this even shows more the truth from my other messages in this
thread.

:-)

Cor

\\\ beside the button it needs now 2 labels.
Private WithEvents timer1 As New System.Timers.Timer
Private Sub Page_Load(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles MyBase.Load
'Put user code to initialize the page here
If Page.IsPostBack Then
Label1.Text = Session.Item("C").ToString
Label2.Text = ViewState("C").ToString
Else
timer1.Interval = 1000
timer1.Start()
Session.Add("C", 1)
ViewState.Add("C", 1)
End If
End Sub
Private Sub Timer1_Elapsed(ByVal sender As System.Object, _
ByVal e As System.Timers.ElapsedEventArgs) Handles timer1.Elapsed
Session.Item("C") = CInt(Viewstate("C")) + 1
Viewstate.Item("C") = CInt(Viewstate("C")) + 1
End Sub
///
 
Back
Top