Mixing javascript and ASP.net code

A

Aussie Rules

Hi.

I am using Live Earth SDK, and have very little Javascript knowledge, but I
need to insert values into a Javascript function, where the vales are in the
ASP.net VB code.

The javascript function is below, but I need to replace the value
(47.6, -122.33) with variables in the code behind page that I get from my
SQL 2000 database.

Is there a way to do this ?


Thanks


function GetMap()
{
map = new VEMap('myMap');
map.LoadMap(new VELatLong(47.6, -122.33), 10 ,'h' ,false);
}
 
G

Guest

Hi Aussie,

I hope you are well. There are two ways of doing it.
1. Use methods offered by ClientScriptManager class
i.e.

protected void Page_Load(object sender, EventArgs e)
{

// these variables should be set to values
// retreived from database
double x = 47.6;
double y = -122.33;

if (!ClientScript.IsClientScriptBlockRegistered(this.GetType(), "mapScript"))
{

string script =
"<script type=\"text/javascript\">\n" +
"function GetMap() {{\n" +
" var map = new VEMap('myMap');\n" +
" map.LoadMap(new VELatLong({0}, {1}), 10 ,'h' ,false);\n" +
"}}\n" +
"</script>";

ClientScript.RegisterClientScriptBlock(
this.GetType(),
"mapScript",
String.Format(script, x, y));
}
}


or storing retreived values in properties and then using <%%> code block on
the aspx page:


-- code behind/beside --

protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
// populate with values from database
XCoordinate = 47.6;
YCoordinate = -122.33;
}
}

// i used viewstate in order to
// not to make database roundtrips on postbacks
protected double XCoordinate
{
get
{
object value = ViewState["XCoordinate"];
return value == null ? 0.0d : (double)value;
}
set
{
ViewState["XCoordinate"] = value;
}
}

protected double YCoordinate
{
get
{
object value = ViewState["YCoordinate"];
return value == null ? 0.0d : (double)value;
}
set
{
ViewState["YCoordinate"] = value;
}
}

-- end code behind/beside --


-- aspx page code --

<script type="text/javascript">

function GetMap()
{
map = new VEMap('myMap');
map.LoadMap(new VELatLong(<%= XCoordinate %>, <%= YCoordinate %>), 10 ,'h'
,false);
}

</script>
-- end aspx page code --


Hope it helps
 
?

=?ISO-8859-1?Q?G=F6ran_Andersson?=

Aussie said:
Hi.

I am using Live Earth SDK, and have very little Javascript knowledge,
but I need to insert values into a Javascript function, where the vales
are in the ASP.net VB code.

The javascript function is below, but I need to replace the value (47.6,
-122.33) with variables in the code behind page that I get from my SQL
2000 database.

Is there a way to do this ?


Thanks


function GetMap()
{
map = new VEMap('myMap');
map.LoadMap(new VELatLong(47.6, -122.33), 10 ,'h' ,false);
}

Put a Literal control where you want the value, and set the Text
property of the control in code behind.
 
B

bruce barker

function GetMap()
{
map = new VEMap('myMap');
map.LoadMap(new VELatLong(<%=codebehindfunc1()%>,
<%=codebehindfunc2()%>),
10 ,'h' ,false);
}

-- bruce (sqlwork.com)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,483
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top