Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

ASP.Net 1.1-- calling Javascript from within a method?

0 views
Skip to first unread message

Steve Hershoff

unread,
Oct 11, 2007, 11:59:25 AM10/11/07
to
Hi everyone,

We have a javascript function we'd like to call from within a C# method in
our code-behind file. The way it has worked historically is we'd call the
method from a hyperlink, like this:

<a href='javascript:MyMethod(Param1, Param2)' runat="server"
ID="MyLink">click here</a>

...what I'd like to do now is somehow call "MyMethod" from the code-behind
file. I've tried something like this but it doesn't do anything, as best I
can tell.

void JavaScriptTest()
{

Response.Write("<script language='javascript'>");
Response.Write("MyMethod(Param1, Param2);");
Response.Write("<"+"/script>");
}

If I replace the MyMethod line above with something like a vanilla
window.alert(), it does work, so I figure I must be close to an answer?


Eliyahu Goldin

unread,
Oct 11, 2007, 12:34:02 PM10/11/07
to
This may help you:
How to Pass Messages and Actions between Server and Client
http://usableasp.net/DeveloperPage.aspx?page=Articles/HowTo/HowToPassMessagesBetweenServerAndClient.htm

--
Eliyahu Goldin,
Software Developer
Microsoft MVP [ASP.NET]
http://msmvps.com/blogs/egoldin
http://usableasp.net


"Steve Hershoff" <babb...@nowhere.com> wrote in message
news:%23mWez%23BDIH...@TK2MSFTNGP06.phx.gbl...

IfThenElse

unread,
Oct 11, 2007, 12:39:16 PM10/11/07
to
You can try

<body onload="JavaScrip: MyMethod(Param1, Param2);">

You need to guarantee that all you controls are available if you function
using any of your controls's IDs. It all depends on what you are doing.

Not sure if onload executes after Body loads or not.

"Steve Hershoff" <babb...@nowhere.com> wrote in message
news:%23mWez%23BDIH...@TK2MSFTNGP06.phx.gbl...

Peter Bromberg [C# MVP]

unread,
Oct 11, 2007, 3:17:00 PM10/11/07
to
Steve,
you could certainly do it this way (there are others, to be sure). But I
think the real problem you have is that you are passing literal string values
of "Param1, Param2" instead of the actual values which would need to be
concatenated:

Response.Write("MyMethod(" +Param1 + "," + Param2 + ");");


See the picture?
Peter

--
Recursion: see Recursion
site: http://www.eggheadcafe.com
unBlog: http://petesbloggerama.blogspot.com
BlogMetaFinder: http://www.blogmetafinder.com

"Steve Hershoff" wrote:

> Hi everyone,
>
> We have a javascript function we'd like to call from within a C# method in
> our code-behind file. The way it has worked historically is we'd call the
> method from a hyperlink, like this:
>
> <a href='javascript:MyMethod(Param1, Param2)' runat="server"
> ID="MyLink">click here</a>
>

> ....what I'd like to do now is somehow call "MyMethod" from the code-behind

Steve Hershoff

unread,
Oct 11, 2007, 4:49:32 PM10/11/07
to
Thanks Peter (and others). I'd be interested in your input on some other
ways of doing this?

As for the parameters being passed in literally, we actually generate the
method call and appropriate params as a big string, using another method, so
that should be ok, but your point is well taken. Thanks!


"Peter Bromberg [C# MVP]" <pbro...@yahoo.yohohhoandabottleofrum.com> wrote
in message news:AA4A62AD-0A56-40AD...@microsoft.com...

0 new messages