How to run a javascrpt function from the server?

R

Rob Meade

Hi all,

I need to be able to run a javascript function on my page (to launch a popup
window) from my .net code which is run after a trigger in an update panel is
launched (Ajax etc)...

Can anyone give me some pointers...

Thanks in advance,

Regards

Rob
 
J

John Saunders

Rob Meade said:
Hi all,

I need to be able to run a javascript function on my page (to launch a
popup window) from my .net code which is run after a trigger in an update
panel is launched (Ajax etc)...

Can anyone give me some pointers...

The server can't run client code.

Please be more explicit about what you're trying to accomplish, and maybe
we'll have some thoughts.

John
 
G

Guest

Rob

I usually do something like this:

string message = "whatever you want it to be";
Page.RegisterClientScriptBlock("messagebox","<script language=javascript>
alert('" + message.Replace("'", "\'").Replace("\n", " ") + "');</script>\n");

Put this code in the code behind event you want to fire.


Let me know if this helps.
 
M

Mark Rae

I usually do something like this:

string message = "whatever you want it to be";
Page.RegisterClientScriptBlock("messagebox","<script language=javascript>
alert('" + message.Replace("'", "\'").Replace("\n", " ") +
"');</script>\n");

Or, in v2...

string message = "whatever you want it to be";
ClientScript.RegisterClientScriptBlock("messagebox","alert('" +
message.Replace("'", "\'").Replace("\n", " ") + "');", true);
 
R

Rob Meade

Thanks for the replies chaps..

Just to check then...

So, if I do that in my code behind, does that actually "launch" the
javascript or just write it to the page ready to be launched if you know
what I mean?

I have found for example that on my linkButton I can specific
"OnClientClick" values, which I've entered for now as a javascript string
which launches a window - alas, I am also setting a session variable on the
onclick (code behind) and the popup is happening just a bit quicker than the
session being set, so the first time its not set, but then if you hit
refresh on the popup its fine...

What I really want I guess is...

code behind - set session, trigger popup to launch...

Will your example(s) do that?

Regards

Rob
 
M

Mark Rae

Oops thanks Mark I forgot that Page.RegisterClientScriptBlock was
deprecated.

And also <script language=javascript>, but we've done that one to death
recently... ;-)
 

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,755
Messages
2,569,535
Members
45,007
Latest member
obedient dusk

Latest Threads

Top