generate clientside javascript from asp.net page

F

fxs

Hello, I've got the following elements on my page:

A Body tag that looks like this:
<body onload="setTabs()">

A Javascript script in the <head> that has:
var foo;
function setTabs()
{
doSomething(foo);
}

I want my ASP.NET page to set the value of foo so that it gets used in the
setTabs() function onload of the page.

So, I tried the following in Page_Load()

string strScript = "<script language='javascript'>foo=" +
Request.QueryString["var"].ToString() + "; </script>";
if(!this.IsClientScriptBlockRegistered("clientScript"))
this.RegisterClientScriptBlock("clientScript", scriptString);

This obviously does not seem to work.I'm trying to therefore figure out, how
do I set the value of a client side javascript variable (foo) based on the
value of my serverside variable request.querystring["var"]?

Any help would be appreciated.

thanks,
fxs
 
G

Guest

Hi

You could write the function like this...

function setTabs()
{
doSomething(<%= qsvar %>);
}

and your Page class should have...

public string qsvar="";

Page_Load()
{
qsvar = Page.Request.QueryString["var"];
}

You might be able to do it with without the variable, by just using <%=
Page.Request.QueryString["var"] %> in your Javascript - haven't tested this
tho!

HTH


Dan
 
E

Eliyahu Goldin

You can call Response.Write (strScript);

But the cleanest way is to pass data in a hidden input instead of setting a
client variable from the server.

Eliyahu
 
F

fxs

I tested <%=request.querystring("var")%> and that works -- however, I;d like
to be able to stay away from the asp style code-insertion tactics if i can.

How about creating a serverside input control type = hidden runat=server and
then trying to assign it a value on the page_load?

dhnriverside said:
Hi

You could write the function like this...

function setTabs()
{
doSomething(<%= qsvar %>);
}

and your Page class should have...

public string qsvar="";

Page_Load()
{
qsvar = Page.Request.QueryString["var"];
}

You might be able to do it with without the variable, by just using <%=
Page.Request.QueryString["var"] %> in your Javascript - haven't tested
this
tho!

HTH


Dan



fxs said:
Hello, I've got the following elements on my page:

A Body tag that looks like this:
<body onload="setTabs()">

A Javascript script in the <head> that has:
var foo;
function setTabs()
{
doSomething(foo);
}

I want my ASP.NET page to set the value of foo so that it gets used in
the
setTabs() function onload of the page.

So, I tried the following in Page_Load()

string strScript = "<script language='javascript'>foo=" +
Request.QueryString["var"].ToString() + "; </script>";
if(!this.IsClientScriptBlockRegistered("clientScript"))
this.RegisterClientScriptBlock("clientScript",
scriptString);

This obviously does not seem to work.I'm trying to therefore figure out,
how
do I set the value of a client side javascript variable (foo) based on
the
value of my serverside variable request.querystring["var"]?

Any help would be appreciated.

thanks,
fxs
 
F

fxs

great. thanks! i'll try that!

Eliyahu Goldin said:
You can call Response.Write (strScript);

But the cleanest way is to pass data in a hidden input instead of setting
a
client variable from the server.

Eliyahu

fxs said:
Hello, I've got the following elements on my page:

A Body tag that looks like this:
<body onload="setTabs()">

A Javascript script in the <head> that has:
var foo;
function setTabs()
{
doSomething(foo);
}

I want my ASP.NET page to set the value of foo so that it gets used in
the
setTabs() function onload of the page.

So, I tried the following in Page_Load()

string strScript = "<script language='javascript'>foo=" +
Request.QueryString["var"].ToString() + "; </script>";
if(!this.IsClientScriptBlockRegistered("clientScript"))
this.RegisterClientScriptBlock("clientScript", scriptString);

This obviously does not seem to work.I'm trying to therefore figure out, how
do I set the value of a client side javascript variable (foo) based on
the
value of my serverside variable request.querystring["var"]?

Any help would be appreciated.

thanks,
fxs
 

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

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,581
Members
45,057
Latest member
KetoBeezACVGummies

Latest Threads

Top