473,466 Members | 1,370 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

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

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?
Oct 11 '07 #1
4 5603
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" <ba******@nowhere.comwrote in message
news:%2******************@TK2MSFTNGP06.phx.gbl...
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?

Oct 11 '07 #2
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" <ba******@nowhere.comwrote in message
news:%2******************@TK2MSFTNGP06.phx.gbl...
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?

Oct 11 '07 #3
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?
Oct 11 '07 #4
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]" <pb*******@yahoo.yohohhoandabottleofrum.comwrote
in message news:AA**********************************@microsof t.com...
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?

Oct 11 '07 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

18
by: Simula | last post by:
I am developing an HTML javascript application and I want to preserve state in a way that can be book-marked. I chose HTML anchors as a means of preserving state. When the application changes...
4
by: Zeebra3 | last post by:
Here goes: I have a web form with several asp:dropdownlists, with which, when selection is changed I want to fire an event defined in some clientside js. The content of the clientside code is...
8
by: Brett Robichaud | last post by:
I understand how code-behind can handle events for a page, but can I call a code-behind method from within a <script> tag in my ASP.Net page, or can I only call methods defined in other <script>...
1
by: Jamie | last post by:
I am using remote scripting to call a .NET page within a main page. The child page (child.aspx) returns html that contains dynamically created javascript methods (function test()). The parent...
4
by: Martin Feuersteiner | last post by:
Dear Group I'm using VB to write an aspnet application. I would like to call a javascript function from within a VB Sub or VB Function, is it possible? My code is something like this: VB...
13
by: ukrbend | last post by:
I'm new to Javascript and to html and am trying to make the following code snippet work but it doesn't. It refuses to call the getPage() function and I always get a 404 error. I know the code is...
3
by: MicroMoth | last post by:
Hi, I'm trying to call a Javascript function within a foreach loop. I am loop over a series of users and I want to call the JS function which opens a new window, passing in the user id to each...
9
by: misdst23 | last post by:
Hi, I know I can call a static java method within javascript by using the <% ... %tags. But how can pass a javascript variable ? function thefunction() { var = javascriptvariable ;
15
by: Sampat | last post by:
Hi, I wanted to know the performance of calling a function pointer v/s a normal function call in javascript in a scenario where there are multiple calls in the js to the same function. Please...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.