473,624 Members | 2,510 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

setting a global variable once and for all

The first time I enter a page, I want to set a variable once, and have that
variable retain its value from that point on. So on the first time in the
program, I set runLoc = "yes". When I then call routine validatex, the
variable retains it's value. But when I call routine validatex from the
button click event, variable runLoc is null. How can I retain the value of
runLoc permanently? thanks
Here's the complete program:

using System;

public partial class scopeTest : System.Web.UI.P age
{
public string runLoc;
protected void Page_Load(objec t sender, EventArgs e)
{
if (!IsPostBack) // first pass:
{
runLoc = "yes";
}
validatex();
}

protected void Button1_Click(o bject sender, EventArgs e)
{
validatex();
}

protected void validatex()
{
string b = runLoc;
}

}

Nov 10 '06 #1
2 1530
Try this:

public string runLoc
{
get{
if(ViewState["runLoc"] != null)
return ViewState["runLoc"].ToString();
else
return string.empty;
}
set
{ ViewState["runLoc"] = value; }
}

That will essentially create a property that save/stores the value in the
viewstate. The only way you can maintain a variable is to use the viewstate.
Of course, any viewstate changes will only occur on postback, but this is
good to get things initially set. You may have to toy with setting the
variable at the right point, in other words before the page saves the
viewstate so you may have to play with the correct event to set it within.

--
Hope this helps,
Mark Fitzpatrick
Former Microsoft FrontPage MVP 199?-2006

"Fred Exley" <fe*******@msn. comwrote in message
news:12******** *****@corp.supe rnews.com...
The first time I enter a page, I want to set a variable once, and have
that
variable retain its value from that point on. So on the first time in the
program, I set runLoc = "yes". When I then call routine validatex, the
variable retains it's value. But when I call routine validatex from the
button click event, variable runLoc is null. How can I retain the value
of
runLoc permanently? thanks
Here's the complete program:

using System;

public partial class scopeTest : System.Web.UI.P age
{
public string runLoc;
protected void Page_Load(objec t sender, EventArgs e)
{
if (!IsPostBack) // first pass:
{
runLoc = "yes";
}
validatex();
}

protected void Button1_Click(o bject sender, EventArgs e)
{
validatex();
}

protected void validatex()
{
string b = runLoc;
}

}

Nov 10 '06 #2
Hey, it worked! thanks much. I was also wondering if setting a session
variable would be appropriate to use, or making a public dataset, but hadn't
yet heard of using a ViewState. In my limited exposure to the .Net
Framework, it seems I can always get whatever I'm trying to do to work, one
way or another, but I'm never sure whether I'm implementing an elegant
solution or an idiotic one. thanks again

Nov 10 '06 #3

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

Similar topics

10
17850
by: Matt | last post by:
Greetings, What are people's thoughts on global variables in C++? Why are we taught not to use them in programming? Is it true that if you are running two copies of the C program one copy can overwrite another copies global variable? I know that you could overwrite a value in a global variable from a function, but you could also do that if you pass the variable in and then out again... so how is that any different?
8
9701
by: David McDivitt | last post by:
I need to set tabs on java generated pages. Pages have four sections: header, sidebar, body, and footer. The sidebar and body change dynamically. The tab key must go to anchors, fields, and buttons doing all in the header first, all in the sidebar second, etc. A base page contains includes for all the pieces and has the body tag. I am trying to use code pasted below. Help would be appreciated. Thanks <script language="javascript"> <!--
13
6994
by: Le, Thanh-Nhan | last post by:
Hi, How can I define a global variable in .NET? I mean, everywhere in the same project can access this variable. Thanks Nhan
41
10656
by: Miguel Dias Moura | last post by:
Hello, I am working on an ASP.NET / VB page and I created a variable "query": Sub Page_Load(sender As Object, e As System.EventArgs) Dim query as String = String.Empty ... query = String.Format("SELECT * FROM dbo.documents WHERE ") & query End Sub
44
3608
by: fabio | last post by:
Why? i' ve heard about this, the usage of global vars instead of locals is discouraged, but why? thx :)
7
2568
by: zeecanvas | last post by:
Hi, First of all: Yes, I know global variables are bad, but I've a huge amount of legacy code, and I've to maintain it _as_is_. I'm maintaining a big program. I moved all (program-wide scope) global variables outside of the files they were defined it, and created some files that just hold global variables definitions (just variables, without any function definition). So, depending on the purpose/category of variables, they're defined...
1
6472
by: laredotornado | last post by:
Hi, I'm using PHP 4.4.4 on Apache 2 on Fedora Core 5. PHP was installed using Apache's apxs and the php library was installed to /usr/local/php. However, when I set my "error_reporting" setting to be "E_ALL", notices are still not getting reported. The perms on my file are 664, with owner root and group root. The php.ini file is located at /usr/local/lib/php/php.ini. Any ideas why the setting does not seem to be having an effect? ...
10
2505
by: Brad Baker | last post by:
I have an asp.net/csharp application that requires a particular variable to work properly. This variable is usually passed via a query string in the URL when the application is first run but under certain circumstances the query string may not contain the variable. So I need some way of establishing a default value if one isn't set. Is there some way I can set a query string on page_load OR is there some way I can use a global variable...
1
29337
weaknessforcats
by: weaknessforcats | last post by:
C++: The Case Against Global Variables Summary This article explores the negative ramifications of using global variables. The use of global variables is such a problem that C++ architects have called it polluting the global namespace. This article explores what happens when the global namespace becomes polluted and how to avoid this condition. The opinions expressed in this article are those of the author alone although many have...
0
8231
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8672
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
8330
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8471
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
5561
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4075
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4167
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
1780
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
2
1474
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.