473,796 Members | 2,544 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Global variable

How can I declare a global variable in my .js file, that I can preserve her
value each time I need to call any function of .JS file in my ASP.NET
application?

Example:
var aux=null;

function myFunction1()
{
...
}

function myFunction2()
{
...
}

Now when I call myFunction1(), aux = true, and when I call myFunction2(),
aux = false. The problem is that I need to preserve the previous value to
test it in both functions. How?
Help me please.

Both functions are called in click event of 2 button's.

--
Programming ASP.NET with VB.NET
Thank's (if you try to help me)
Hope this help you (if I try to help you)
ruca
Nov 18 '05 #1
9 2544
I wouldn't personally expect variables of this type to survive a postback any more than I would expect lemmings to survive a tidal wave.
Nov 18 '05 #2
It should work. Try :

var v;
function a(){v=true}
function b(){alert(b)}
a();
b(); // Should display true

Dont' you have a local variable that uses the same name ?
Do you have a postaback between these two calls ? (that would reinitialize
of course the variable value though it shouldn't be false).

Patrice
"ruca" <ru***@iol.pt > a écrit dans le message de
news:%2******** ********@TK2MSF TNGP11.phx.gbl. ..
How can I declare a global variable in my .js file, that I can preserve her value each time I need to call any function of .JS file in my ASP.NET
application?

Example:
var aux=null;

function myFunction1()
{
...
}

function myFunction2()
{
...
}

Now when I call myFunction1(), aux = true, and when I call myFunction2(),
aux = false. The problem is that I need to preserve the previous value to
test it in both functions. How?
Help me please.

Both functions are called in click event of 2 button's.

--
Programming ASP.NET with VB.NET
Thank's (if you try to help me)
Hope this help you (if I try to help you)
ruca

Nov 18 '05 #3
Hi Patrice,
Dont' you have a local variable that uses the same name ? No.
Do you have a postaback between these two calls ? (that would reinitialize
of course the variable value though it shouldn't be false). I think that what happens is that when I click on button the page load
again, right?
But If I have a link (with href=...) instead of a button (click event) the
thing works fine.

But in Load event I have:

if not Page.IsPostBack () then
...
end if
--
Programming ASP.NET with VB.NET
Thank's (if you try to help me)
Hope this help you (if I try to help you)
ruca
"Patrice" <no****@nowhere .com> escreveu na mensagem
news:uI******** ******@tk2msftn gp13.phx.gbl... It should work. Try :

var v;
function a(){v=true}
function b(){alert(b)}
a();
b(); // Should display true

Dont' you have a local variable that uses the same name ?
Do you have a postaback between these two calls ? (that would reinitialize
of course the variable value though it shouldn't be false).

Patrice
"ruca" <ru***@iol.pt > a écrit dans le message de
news:%2******** ********@TK2MSF TNGP11.phx.gbl. ..
How can I declare a global variable in my .js file, that I can preserve

her
value each time I need to call any function of .JS file in my ASP.NET
application?

Example:
var aux=null;

function myFunction1()
{
...
}

function myFunction2()
{
...
}

Now when I call myFunction1(), aux = true, and when I call myFunction2(), aux = false. The problem is that I need to preserve the previous value to test it in both functions. How?
Help me please.

Both functions are called in click event of 2 button's.

--
Programming ASP.NET with VB.NET
Thank's (if you try to help me)
Hope this help you (if I try to help you)
ruca


Nov 18 '05 #4
ASP.NET simulates the usual statefull event driven programming model but
it's worth to note that the server side "model" and what you have client
side is not the same "thing". The server side "model" is used to create the
client side code (mostly HTML+JavaScript snippets) that will reproduce what
you modelled server side. Client-side you'll have also some info (the
viewstate) that allows to recreate automatically the server side model from
scratch during the next server round trip.

In your case, you initialize a variable client side code, then you have a
postback. The server side "model" is recreated to regenerate client side
code (possibly updated depending on what you do on postback). When the new
code is returned to the navigator the client side variable is initialized
again.

This variable should be persisted beetween calls (for example using an
hidden field). You should find more details on the general architecture in
the documentation.

Patrice
"ruca" <ru***@iol.pt > a écrit dans le message de
news:%2******** **********@TK2M SFTNGP12.phx.gb l...
Hi Patrice,
Dont' you have a local variable that uses the same name ?

No.
Do you have a postaback between these two calls ? (that would reinitialize
of course the variable value though it shouldn't be false).

I think that what happens is that when I click on button the page load
again, right?
But If I have a link (with href=...) instead of a button (click event) the
thing works fine.

But in Load event I have:

if not Page.IsPostBack () then
...
end if
--
Programming ASP.NET with VB.NET
Thank's (if you try to help me)
Hope this help you (if I try to help you)
ruca
"Patrice" <no****@nowhere .com> escreveu na mensagem
news:uI******** ******@tk2msftn gp13.phx.gbl...
It should work. Try :

var v;
function a(){v=true}
function b(){alert(b)}
a();
b(); // Should display true

Dont' you have a local variable that uses the same name ?
Do you have a postaback between these two calls ? (that would reinitialize of course the variable value though it shouldn't be false).

Patrice
"ruca" <ru***@iol.pt > a écrit dans le message de
news:%2******** ********@TK2MSF TNGP11.phx.gbl. ..
How can I declare a global variable in my .js file, that I can
preserve her
value each time I need to call any function of .JS file in my ASP.NET
application?

Example:
var aux=null;

function myFunction1()
{
...
}

function myFunction2()
{
...
}

Now when I call myFunction1(), aux = true, and when I call

myFunction2(), aux = false. The problem is that I need to preserve the previous value to test it in both functions. How?
Help me please.

Both functions are called in click event of 2 button's.

--
Programming ASP.NET with VB.NET
Thank's (if you try to help me)
Hope this help you (if I try to help you)
ruca



Nov 18 '05 #5
But why this works with links, and not with buttons? This is what I can't
understand.
--
Programming ASP.NET with VB.NET
Thank's (if you try to help me)
Hope this help you (if I try to help you)
ruca
"Patrice" <no****@nowhere .com> escreveu na mensagem
news:OX******** ******@TK2MSFTN GP12.phx.gbl...
ASP.NET simulates the usual statefull event driven programming model but
it's worth to note that the server side "model" and what you have client
side is not the same "thing". The server side "model" is used to create the client side code (mostly HTML+JavaScript snippets) that will reproduce what you modelled server side. Client-side you'll have also some info (the
viewstate) that allows to recreate automatically the server side model from scratch during the next server round trip.

In your case, you initialize a variable client side code, then you have a
postback. The server side "model" is recreated to regenerate client side
code (possibly updated depending on what you do on postback). When the new
code is returned to the navigator the client side variable is initialized
again.

This variable should be persisted beetween calls (for example using an
hidden field). You should find more details on the general architecture in
the documentation.

Patrice
"ruca" <ru***@iol.pt > a écrit dans le message de
news:%2******** **********@TK2M SFTNGP12.phx.gb l...
Hi Patrice,
Dont' you have a local variable that uses the same name ?

No.
Do you have a postaback between these two calls ? (that would reinitialize of course the variable value though it shouldn't be false).

I think that what happens is that when I click on button the page load
again, right?
But If I have a link (with href=...) instead of a button (click event) the
thing works fine.

But in Load event I have:

if not Page.IsPostBack () then
...
end if
--
Programming ASP.NET with VB.NET
Thank's (if you try to help me)
Hope this help you (if I try to help you)
ruca
"Patrice" <no****@nowhere .com> escreveu na mensagem
news:uI******** ******@tk2msftn gp13.phx.gbl...
It should work. Try :

var v;
function a(){v=true}
function b(){alert(b)}
a();
b(); // Should display true

Dont' you have a local variable that uses the same name ?
Do you have a postaback between these two calls ? (that would reinitialize of course the variable value though it shouldn't be false).

Patrice
"ruca" <ru***@iol.pt > a écrit dans le message de
news:%2******** ********@TK2MSF TNGP11.phx.gbl. ..
> How can I declare a global variable in my .js file, that I can preserve her
> value each time I need to call any function of .JS file in my ASP.NET > application?
>
> Example:
> var aux=null;
>
> function myFunction1()
> {
> ...
> }
>
> function myFunction2()
> {
> ...
> }
>
> Now when I call myFunction1(), aux = true, and when I call

myFunction2(),
> aux = false. The problem is that I need to preserve the previous

value to
> test it in both functions. How?
> Help me please.
>
> Both functions are called in click event of 2 button's.
>
> --
> Programming ASP.NET with VB.NET
> Thank's (if you try to help me)
> Hope this help you (if I try to help you)
> ruca
>
>



Nov 18 '05 #6
I see that with web control button it's impossible topreserve the value of
my global variable in .JS file, right?
I accept any sugestions, if anyone can help me!!!
--
Programming ASP.NET with VB.NET
Thank's (if you try to help me)
Hope this help you (if I try to help you)
ruca

"Bin Song, MCP" <an*******@disc ussions.microso ft.com> escreveu na mensagem
news:42******** *************** ***********@mic rosoft.com...
Hi, Ruca,

Because ASP.NET buttons do postback and links don't.
If you only need the buttons to do client side work and don't need to submit data back to server, you might just use html input buttons instead.
Bin Song, MCP

Nov 18 '05 #7
Hi

Web pages don't preserve any page variables
You have to do through Session, Application, ViewState or Cookie

Bin Song, MCP
Nov 18 '05 #8
There's a way around this, thought its not direct, depending on the context
of your "Global" definition.

You can access Cookie values through JavaScript. If you manage a variable
by always referring to a value inside of the client's cookie, then that
should work. (Global within a User's context, as opposed to a session
context)

You could, instead of using a js variable (var x;) always refer to a value
that is inside of a hidden field (make sure that the hidden field is set to
runat="server") . This would be "global" within a given page context.

You could subclass your page class. Create a Class, deriving from page,
that has a hidden field. When the page posts back, you perform some sort of
synch operation with that hidden field and an application/Session variable
and expose that value as a property of the page. That way all of the pages
that derive from this subclass would automatically have access to that
variable (effectively creates a "Global" Session/Applicatoin) level value
that script can access). Side Note...if you subclass your Page class,
you'll want to place the subclass inside of a seperate assembly and set a
reference to it in the Visual Studio IDE, otherwise the designer gets a
little funky.

If there's any way around doing this, I'd recommend it. Take a step back
and see if there's something you can do to your architecture to make this
unnecessary. However, these approaches should work if you have to take the
approach you're looking at.

"ruca" <ru***@iol.pt > wrote in message
news:%2******** ********@TK2MSF TNGP11.phx.gbl. ..
How can I declare a global variable in my .js file, that I can preserve her value each time I need to call any function of .JS file in my ASP.NET
application?

Example:
var aux=null;

function myFunction1()
{
...
}

function myFunction2()
{
...
}

Now when I call myFunction1(), aux = true, and when I call myFunction2(),
aux = false. The problem is that I need to preserve the previous value to test it in both functions. How?
Help me please.

Both functions are called in click event of 2 button's.

--
Programming ASP.NET with VB.NET
Thank's (if you try to help me)
Hope this help you (if I try to help you)
ruca

Nov 18 '05 #9
How can I get and set a value for a Session variable in a .JS file?
Like this in VB:

To set:
Session.Item("p opup") = true //means that is already a popup opened
Session.Item("p opup") = false //means that there is no popup opened

To get:
bPopup = Session.Item("p opup")
--
Programming ASP.NET with VB.NET
Thank's (if you try to help me)
Hope this help you (if I try to help you)
ruca

"Bin Song, MCP" <an*******@disc ussions.microso ft.com> escreveu na mensagem
news:C3******** *************** ***********@mic rosoft.com...
Hi,

Web pages don't preserve any page variables.
You have to do through Session, Application, ViewState or Cookie.

Bin Song, MCP

Nov 18 '05 #10

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

Similar topics

8
102758
by: David Hitillambeau | last post by:
Hi guys, As I am new to Python, i was wondering how to declare and use global variables. Suppose i have the following structure in the same module (same file): def foo: <instructions> <instructions> def bar: <instructions>
4
24188
by: Andrew V. Romero | last post by:
I have been working on a function which makes it easier for me to pull variables from the URL. So far I have: <script language="JavaScript"> var variablesInUrl; var vArray = new Array(); function loadUrlVariables() { varString = location.search;
4
7149
by: Dan Elliott | last post by:
Hello, Converting from a working C program to C++, I run into the following error: I have a header: (header.h) namespace shared{ ... struct X{ ...
2
8833
by: Thomas Matthews | last post by:
Hi, I'm getting linking errors when I declare a variable in the global scope, but not inside a function. The declarations are the same (only the names have been changed...). class Book { public: Book()
8
2560
by: lawrence | last post by:
I'm learning Javascript. I downloaded a script for study. Please tell me how the variable "loop" can have scope in the first function when it is altered in the second function? It is not defined in global space, therefore it is not a global variable, yes? Even if it was global, how would it get from one function to another? In PHP variables are copied by value. Are they copied by reference in Javascript? <SCRIPT LANGUAGE="JavaScript">
17
5634
by: MLH | last post by:
A97 Topic: If there is a way to preserve the values assigned to global variables when an untrapped runtime error occurs? I don't think there is, but I thought I'd ask. During development, I'm constantly running tests on imperfect code. On of the cumbersome jobs encountered is reassigning global vars their values after a close encounter with an untrapped runtime error. Rather than writing a procedure to simply reassign them all with a...
10
2663
by: Charles O'Flynn | last post by:
As a complete newcomer (2-3 days) to PHP, although not to programming in general, I have 'dived in' to start a small project to read and parse an XML data stream. I have already worked out most of the more specialist aspects of the job but am now completely stuck on something I would have thought were simplicity itself... I need to have a large number of global variables visible inside functions - it's not possible to pass them into the...
9
3035
by: Ed Jensen | last post by:
I'm having a vexing problem with global variables in Python. Please consider the following Python code: #! /usr/bin/env python def tiny(): bar = for tmp in foo: bar.append(tmp) foo = bar
1
29385
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...
112
5486
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 that may print some messages. foo(...) { if (!silent)
0
9680
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...
1
10174
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
9052
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7548
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5442
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
5575
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4118
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3731
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2926
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.