Wrox Programmer Forums
Go Back   Wrox Programmer Forums > ASP.NET and ASP > ASP.NET 1.0 and 1.1 > ASP.NET 1.1
|
ASP.NET 1.1 As of 10/6/2005, this forum is locked as part of the reorganization described here: http://p2p.wrox.com/topic.asp?TOPIC_ID=35394. No posts have been deleted. Open ongoing discussions from the last week have been moved to either ASP.NET 1.0 and 1.1 Beginners http://p2p.wrox.com/asp-net-1-0-1-1-basics-60/ or ASP.NET 1.0 and 1.1 Professional. http://p2p.wrox.com/forum.asp?FORUM_ID=50. See my sticky post inside for more.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the ASP.NET 1.1 section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old August 23rd, 2005, 06:59 AM
Authorized User
 
Join Date: Mar 2004
Posts: 31
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via MSN to mistry_bhavin
Default Save variable's value between page roundtrip

Dim i As Int16

    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 (Not IsPostBack) Then
            i = 10
        End If

    End Sub

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Response.Write(i)
    End Sub


When I write this kind of stuff I get 0 as a value of i.
But if i do not write i = 10 inside the if(not ispostback) then
i get proper value.
So is it nessary to set value for variables every time I load the page? Is't there any way to store variables permanantly, which can
hold their values between page roundtrips?

 
Old August 23rd, 2005, 11:24 AM
Friend of Wrox
 
Join Date: Nov 2003
Posts: 1,348
Thanks: 0
Thanked 5 Times in 5 Posts
Default

Where is the variable i declared????

 
Old August 23rd, 2005, 11:39 AM
Authorized User
 
Join Date: Mar 2004
Posts: 31
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via MSN to mistry_bhavin
Default

I is Globaly declared before Page_Load procedure.

 
Old August 23rd, 2005, 12:10 PM
Imar's Avatar
Wrox Author
 
Join Date: Jun 2003
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 Posts
Default

You can store them in ViewState so they survive postbacks:

In Page_Load:
Code:
i = 10
ViewState("MyVar") = i
Then in Button1_Click:
Code:
If Not ViewState("MyVar") Is Nothing Then
  i = Convert.ToIn16(ViewState("MyVar"))
End If
HtH,

Imar
---------------------------------------
Imar Spaanjaars
Everyone is unique, except for me.
 
Old August 24th, 2005, 02:31 AM
Friend of Wrox
 
Join Date: May 2003
Posts: 229
Thanks: 0
Thanked 0 Times in 0 Posts
Default

You can also use session to store.

Charul Shukla
 
Old September 3rd, 2005, 03:47 AM
Friend of Wrox
 
Join Date: Oct 2003
Posts: 336
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via MSN to alyeng2000
Default

viewstate is prefeared than session when doing postback in the same page

Ahmed Ali
Software Developer
 
Old September 3rd, 2005, 08:03 AM
Friend of Wrox
 
Join Date: Oct 2004
Posts: 109
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via Yahoo to anubhav.kumar
Default

 Basically variables which are needed across different page visits (navigation) are to stored in session while viewstate is preffered for page level variables which are excessed across the page visit of the same page( post back)

Anubhav Kumar





Similar Threads
Thread Thread Starter Forum Replies Last Post
how to refresh page after save dialog derickloo ASP.NET 1.0 and 1.1 Basics 0 December 13th, 2007 11:43 PM
How do you save an html page on your local hd? rsalah Javascript How-To 16 January 27th, 2005 01:48 AM
blocking roundtrip javascript jaisonkmathews General .NET 3 November 27th, 2004 12:20 AM
Save the Confirmation Page as a file kelcontech Classic ASP Basics 8 May 13th, 2004 08:32 AM
html page save to excel format mateenmohd Javascript 2 January 17th, 2004 05:48 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.