473,324 Members | 2,473 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,324 software developers and data experts.

variable scope and persistence

Hello...
I assign a variable from the querystring to a class level private variable
as follows

public class CrsInstnc : System.Web.UI.Page {
private string tcoRef = "";

private void Page_Load(object sender, System.EventArgs e) {
if(!IsPostBack){
if(Request.QueryString["ref"] != null){
this.tcoRef = Request.QueryString["ref"].ToString();
}
...
}
}

Every time the page posts back to itself the variable this.tcoRef is empty.
Am I doing something wrong? When the page posts back to itself does the
whole class start from scratch? Should I store the information elsewhere?
Thanks
Nov 19 '05 #1
3 1402
> Every time the page posts back to itself the variable this.tcoRef is
empty.
Am I doing something wrong? When the page posts back to itself does the
whole class start from scratch? Exactly, this is how web applications work.
Should I store the information elsewhere?

Yes, in a session variable.

Eliyahu
Nov 19 '05 #2
Cole Trickle wrote:
Every time the page posts back to itself the variable this.tcoRef is empty.
Am I doing something wrong? When the page posts back to itself does the
whole class start from scratch? Should I store the information elsewhere?

The class is not persisted between postbacks. You must store the value
in either the session state or in the page's view state to persist it
between postbacks.

To stor a value in and retrieve it from the session state do this (C#):
if (Page.IsPostBack) {
// Read from session state
myString=(string) Session["MyValue"];
} else {
// Store in session state
Session["MyValue"] = myString;
}

To store a value in and retrieve it from the view state do this (C#):
if (Page.IsPostBack) {
// Read from view state
myString=(string) ViewState["MyValue"];
} else {
// Store in view state
ViewState["MyValue"] = myString;
}

Anders Norås
http://dotnetjunkies.com/weblog/anoras/
Nov 19 '05 #3
Or, try this....

public class CrsInstnc : System.Web.UI.Page {
private const string vs_TcoRef = "TcoRef";

private void Page_Load(object sender, System.EventArgs e) {
if(!IsPostBack){
if(Request.QueryString["ref"] != null){
this.tcoRef = Request.QueryString["ref"].ToString();
}
...
}

protected string TcoRef{
get{
return
(this.ViewState[vs_TcoRef]==null)?string.Empty:(string)this.ViewState[vs_Tco
Ref]);
}
set{
this.ViewState[vs_TcoRef]=value;
}
}

"Cole Trickle" <co************@hotmail.com> wrote in message
news:cs*******************@news.demon.co.uk...
Hello...
I assign a variable from the querystring to a class level private variable
as follows
Every time the page posts back to itself the variable this.tcoRef is empty. Am I doing something wrong? When the page posts back to itself does the
whole class start from scratch? Should I store the information elsewhere?
Thanks

Nov 19 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Grant Wagner | last post by:
Given the following working code: function attributes() { var attr1 = arguments || '_'; var attr2 = arguments || '_'; return ( function (el1, el2) { var value1 = el1 + el1; var value2 = el2...
1
by: Chris | last post by:
I'm writing an asp page with C# codebehind. I create a page level variable. I then alter the variable by assigning a new value in a private method. When I access the vairable again from another...
10
by: Simon Harvey | last post by:
Hi everyone, Can anyone tell me if I declare a global variable in my pages code behind, is it persisted if the page does a post back, or do I need to add the object to the session object in...
23
by: Russ Chinoy | last post by:
Hi, This may be a totally newbie question, but I'm stumped. If I have a function such as: function DoSomething(strVarName) { ..... }
5
by: strawberry | last post by:
In the function below, I'd like to extend the scope of the $table variable such that, once assigned it would become available to other parts of the function. I thought 'global $table;' would solve...
1
pbmods
by: pbmods | last post by:
VARIABLE SCOPE IN JAVASCRIPT LEVEL: BEGINNER/INTERMEDIATE (INTERMEDIATE STUFF IN ) PREREQS: VARIABLES First off, what the heck is 'scope' (the kind that doesn't help kill the germs that cause...
0
MMcCarthy
by: MMcCarthy | last post by:
We often get questions on this site that refer to the scope of variables and where and how they are declared. This tutorial is intended to cover the basics of variable scope in VBA for MS Access. For...
112
by: istillshine | last post by:
When I control if I print messages, I usually use a global variable "int silent". When I set "-silent" flag in my command line parameters, I set silent = 1 in my main.c. I have many functions...
0
myusernotyours
by: myusernotyours | last post by:
Hi all, Am trying to create a Java Desktop App that uses Java Persistence in Netbeans. The database is MS Access but I tried with Mysql and got the same error. When I run the app( Create the...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.