PC Review


Reply
Thread Tools Rating: Thread Rating: 1 votes, 1.00 average.

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

 
 
Steve Hershoff
Guest
Posts: n/a
 
      11th Oct 2007
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?


 
Reply With Quote
 
 
 
 
Eliyahu Goldin
Guest
Posts: n/a
 
      11th Oct 2007
This may help you:
How to Pass Messages and Actions between Server and Client
http://usableasp.net/DeveloperPage.a...rAndClient.htm

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


"Steve Hershoff" <(E-Mail Removed)> wrote in message
news:%23mWez%(E-Mail Removed)...
> 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?
>



 
Reply With Quote
 
IfThenElse
Guest
Posts: n/a
 
      11th Oct 2007
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" <(E-Mail Removed)> wrote in message
news:%23mWez%(E-Mail Removed)...
> 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?
>



 
Reply With Quote
 
=?Utf-8?B?UGV0ZXIgQnJvbWJlcmcgW0MjIE1WUF0=?=
Guest
Posts: n/a
 
      11th Oct 2007
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
> 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?
>
>
>

 
Reply With Quote
 
Steve Hershoff
Guest
Posts: n/a
 
      11th Oct 2007
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]" <(E-Mail Removed)> wrote
in message news:AA4A62AD-0A56-40AD-83D7-(E-Mail Removed)...
> 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
>> 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?
>>
>>
>>



 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
help needed calling C# method from javascript =?Utf-8?B?TW9yaQ==?= Microsoft ASP .NET 4 20th Jul 2005 06:41 PM
Calling a c# method from Javascript Steve W Microsoft C# .NET 1 8th Apr 2004 04:21 AM
Re: calling codebehind method from javascript Konrad Rotuski Microsoft ASP .NET 0 31st Mar 2004 05:35 PM
calling asp.net method from javascript Scott M. Microsoft ASP .NET 5 24th Mar 2004 11:04 PM
calling cs method from javascript Benny Microsoft ASP .NET 1 10th Nov 2003 09:53 AM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 11:20 AM.