467,923 Members | 1,262 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 467,923 developers. It's quick & easy.

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
  • viewed: 2240
Share:
9 Replies
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****************@TK2MSFTNGP11.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**************@tk2msftngp13.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****************@TK2MSFTNGP11.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******************@TK2MSFTNGP12.phx.gbl...
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**************@tk2msftngp13.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****************@TK2MSFTNGP11.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**************@TK2MSFTNGP12.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******************@TK2MSFTNGP12.phx.gbl...
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**************@tk2msftngp13.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****************@TK2MSFTNGP11.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*******@discussions.microsoft.com> escreveu na mensagem
news:42**********************************@microsof t.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****************@TK2MSFTNGP11.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("popup") = true //means that is already a popup opened
Session.Item("popup") = false //means that there is no popup opened

To get:
bPopup = Session.Item("popup")
--
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*******@discussions.microsoft.com> escreveu na mensagem
news:C3**********************************@microsof t.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 discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

8 posts views Thread by David Hitillambeau | last post: by
4 posts views Thread by Andrew V. Romero | last post: by
4 posts views Thread by Dan Elliott | last post: by
2 posts views Thread by Thomas Matthews | last post: by
10 posts views Thread by Charles O'Flynn | last post: by
9 posts views Thread by Ed Jensen | last post: by
112 posts views Thread by istillshine | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.