Javascript Variable

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

I have a boolean variable defined in the aspx.cs code for my webpage. I need
to read the value of this variable in the javascript code that i have in the
codebehind file. Is there a way to do this??

I've tried the following but I get an error saying that "edit_clicked is
undefined."

if (edit_clicked == false)
<do what i have to do...>
 
Parveen,

You can not do this. Basically, javascript runs on the client side, and
the C# code runs on the server side. If you want to pass information
between them, then you will have to have the server side write the variable
value when it is declared, and have the client issue a postback when the
value changes (so the server can process it).

All in all, not a trivial operation. What are you trying do to?
 
hi,

I think you have a little confusion between server side and client side,
c# code runs in the server, in the client it runs javascript ( most of the
time is like this anyway) , sometimes you need to interact and there are
several ways of doing it, you coudl trigger an action from the server side
by setting a variable that will be read in javascript like this:

in the aspx page :
<script>
var WhatToDo="";
function DoWhatYouHaveToDo()
{
if (WhatToDo!="")
DoSomething();
}
</script>
</HEAD>
<body onload="DoWhatYouHaveToDo();">


then in the code behind you can do this:

Controls.Add( new LiteralControl("<script>WhatToDo='DoIt';</script>) );

it will add that piece of code at the end of the page so you will have the
variable with a value assigned when you run the function.


Is this what you want?

Cheers,
 
Thanks for your reply...I'm trying to control when to show an alert box for
the webpage. This decision is based on the value of the C# variable...does
this make sense?

Nicholas Paldino said:
Parveen,

You can not do this. Basically, javascript runs on the client side, and
the C# code runs on the server side. If you want to pass information
between them, then you will have to have the server side write the variable
value when it is declared, and have the client issue a postback when the
value changes (so the server can process it).

All in all, not a trivial operation. What are you trying do to?

--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

Parveen said:
I have a boolean variable defined in the aspx.cs code for my webpage. I
need
to read the value of this variable in the javascript code that i have in
the
codebehind file. Is there a way to do this??

I've tried the following but I get an error saying that "edit_clicked is
undefined."

if (edit_clicked == false)
<do what i have to do...>
 
Back
Top