Click here to Skip to main content
14,773,753 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
See more:
Hi, i am beginner at c# and asp.net. I have a public decimal variable on in my code behind. How do iget this variable and use it in a if statement with javascript on my aspx page?...Any help will be much appreciated.
Posted

Well there is one shortcut we used to use during the ASP days, this still works in ASP.NET
In the page codebehind declare a public property say public int MyInteger
In the aspx put this

<script>
    function GetMyValue()
    {
     var someVar = <%=this.MyInteger%>;
     //Do something
    }
</script>


To do it nicely, you can use RegisterClientScriptBlock [^]

string script = string.Format("var myVariable = '{0}';", vaiable.ToString());
if (!ClientScript.IsClientScriptBlockRegistered("myScript"))
{
    ClientScript.RegisterClientScriptBlock(typeof(_Default), "myScript", script, true);
}
   
Comments
Ganesh Vellanki 13-Nov-14 5:40am
   
chrome says unidentified syntax, do we need to put quotes...?
public decimal Price;


then you can access that from Javascript as

var price = <%=this.Price%>;
   
v3
Comments
Ruwaldo 6-Jul-12 10:27am
   
tried this already get an error.
DamithSL 6-Jul-12 10:36am
   
What is the error?
Manas Bhardwaj 6-Jul-12 10:27am
   
ditto +5!
DamithSL 6-Jul-12 10:38am
   
:) There is no auto refresh or alert system when someone else added answer. Sorry yes it is ditto......... 5+ from me
Manas Bhardwaj 6-Jul-12 10:41am
   
Nothing to be sorry about. Infact, you posted 1 min before me.
Need to work on my typing skills ;)
Ruwaldo 9-Jul-12 4:24am
   
It says expression expected.
public int i;
i=10;
//do some code....


on view page....
write with in jquery function or any event
var price = <%=this.i%>;
   
Comments
Sandeep Mewara 7-Jul-12 5:44am
   
Why are you simply doing almost a copy-paste of same answer posted before? Avoid it.
Ganesh Vellanki 13-Nov-14 5:40am
   
why says syntax not identified...?
if possible (is available) try to assign value in java to document.cookie = javavalue and then get it from codebehind var x= document.cookie (either via webbrowser or page)
   

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900