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

S

Steve Hershoff

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?
 
I

IfThenElse

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.
 
G

Guest

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
 
S

Steve Hershoff

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!
 

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,480
Members
44,900
Latest member
Nell636132

Latest Threads

Top