Javascript function called from code behind file

M

moni

Hi..

I am trying to use javascript for google maps display.


If I call the javascript function from my aspx file I use:


<input type="text" id="addresstext" value="Huntington Avenue,
Boston, MA" name="yourName" style="width: 287px" />
<input type="button" id="addressclick" value="Display Map"
onclick="showAddress(addresstext.value)" /><br />


and it works.


But using the Response.Write from the aspx.cs file it
does not display it. I need to pass an argument to the function
call.Am I passing the value wrongly??


I am calling it this way:


Response.Write("<script
language='javascript'>showAddress("+getAddress+");</script>");


where, getAddress is the comma-seperated Address string.


This is not displaying the map, I am creating using Javascript.

I have also tried all combinations of singel quotes, and double quotes
wrt calling the javascript function


Thx
 
G

Guest

Most likely that Response.Write has rendered your JavaScript block outside
HTML-tag.
Best way to add JavaScripts from codebehind is to use
RegisterClientScriptBlock

this.RegisterClientScriptBlock("jsGoogleMap","<script
language='javascript'>showAddress("+getAddress+"</script>");
 
B

Ben Rush

Oh....yeah, don't inject javascript into your ASPX page using
Response.Write, there are much better methods for doing this. Have a look at
RegisterStartupScript, etc. Those are going to work with your ASP.net page
infrastructure. What you're doing is going to place script *outside* HTML
blocks and will probably never work - and if it does, it's by chance.
 
K

Kevin Spencer

Note that in ASP.Net 2.0, this method (and related) has been superceded by
the ClientScriptManager's methods for doing the same things. The
ClientScriptManager is a property of the System.Web.UI.Page class, and is
exposed in the Page.ClientScript property. See:

http://msdn2.microsoft.com/en-us/library/system.web.ui.page.clientscript(VS.80).aspx
http://msdn2.microsoft.com/en-us/library/system.web.ui.clientscriptmanager.aspx

--
HTH,

Kevin Spencer
Microsoft MVP

Printing Components, Email Components,
FTP Client Classes, Enhanced Data Controls, much more.
DSI PrintManager, Miradyne Component Libraries:
http://www.miradyne.net
 

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

Top