How to run a javascrpt function from the server?

  • Thread starter Thread starter Rob Meade
  • Start date Start date
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
 
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
 
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.
 
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);
 
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
 
Oops thanks Mark I forgot that Page.RegisterClientScriptBlock was
deprecated.

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