473,753 Members | 8,053 Online
Bytes | Software Development & Data Engineering Community
+ 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='javascrip t:MyMethod(Para m1, Param2)' runat="server"
ID="MyLink">cli ck 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='javas cript'>");
Response.Write( "MyMethod(Param 1, 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 5617
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******@nowhe re.comwrote in message
news:%2******** **********@TK2M SFTNGP06.phx.gb l...
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='javascrip t:MyMethod(Para m1, Param2)' runat="server"
ID="MyLink">cli ck 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='javas cript'>");
Response.Write( "MyMethod(Param 1, 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="JavaScr ip: 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******@nowhe re.comwrote in message
news:%2******** **********@TK2M SFTNGP06.phx.gb l...
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='javascrip t:MyMethod(Para m1, Param2)' runat="server"
ID="MyLink">cli ck 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='javas cript'>");
Response.Write( "MyMethod(Param 1, 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='javascrip t:MyMethod(Para m1, Param2)' runat="server"
ID="MyLink">cli ck 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='javas cript'>");
Response.Write( "MyMethod(Param 1, 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*******@yaho o.yohohhoandabo ttleofrum.comwr ote
in message news:AA******** *************** ***********@mic rosoft.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='javascrip t:MyMethod(Para m1, Param2)' runat="server"
ID="MyLink">cl ick 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='javas cript'>");
Response.Write( "MyMethod(Param 1, 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
12698
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 state, the HTML page URL would change from default.html to default.html#stateA. example: http://pearstudios.com/javascript/locationHashAndFlash.html This has worked perfectly within HTML and javascript alone, however, when connecting Flash and...
4
4086
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 dependant on data collected in the code behind on the server. I have set AutoPostback to false for the controls and added lines such as cboMyCombo1.Attributes.Add("onchange", "MyCombos_OnChange('1')"); in the Page_Load event, which is rendered OK...
8
5360
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> sections? I can't seem to figure out the syntax for for calling code-behind directly. The method is within the class my page inherits from and is public, but when I try to call it from my page I get this error: CS1520: Class, struct, or...
1
2767
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 page sets a document item's innerHTML property to the result of the child page. This all works fine, except that the parent page also makes a call to the "test()" method that is contained within that innerHTML. The call is made after the...
4
6334
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 Code: Public Sub VBProcedure() .. ..
13
6711
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 getting as far as the iframe section as other parts of the html code work correctly. But it is not executing any code within the getPage() function as some "hello world" writes within getPage() wouldn't print. ...
3
3116
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 call for the JS function. So far I've done this: foreach (USER _user in users) { string jscriptString = "<script language='JavaScript'>";
9
41722
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
4284
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 share your thoughts if someone has worked on this. Thanks, Sampat.
0
9072
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9653
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9451
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
9421
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9333
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8328
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
6151
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 then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4771
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 last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
2
2872
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.