Avatar of BeginningWebDesign
BeginningWebDesign

asked on 

Professional way to embed javascript in aspx.cs file

Hi
I currently embed the javascript in my aspx.cs file as follows:
protected void Page_Load(object sender, EventArgs e)
    {
       
            DisplayWeather(pCityName);
            Literal1.Text = "<script src=\"http://maps.google.com/maps?file=api&v=2&key=****" + "\n";
            Literal1.Text += "type=\"text/javascript\"></script>" + "\n";
            Literal1.Text += "<script type=\"text/javascript\">" + "\n";
            Literal1.Text +="//<![CDATA[\"\n";
            Literal1.Text += "function load() {" + "\n";
            Literal1.Text += "if (GBrowserIsCompatible()) {" + "\n";
            Literal1.Text += "var map = new GMap2(document.getElementById(\"map\"))" + "\n";
            Literal1.Text += "map.setMapType(G_NORMAL_MAP);" + "\n";
            Literal1.Text += "map.setCenter(new GLatLng(" + lat.ToString() + ", " + log.ToString() + "), 5); " + "\n";
            Literal1.Text += "map.addControl(new GSmallMapControl());" + "\n";
            Literal1.Text += "map.addControl(new GMapTypeControl());" + "\n";
            Literal1.Text += "map.openInfoWindow(map.getCenter()," + "\n";
            Literal1.Text += "document.createTextNode(\"City\"));" + "\n";
            Literal1.Text += "}" + "\n";
            Literal1.Text += "}";
            Literal1.Text += "//]]>\n";
            Literal1.Text += "</script>";
    }

How would i do the above correctly, the above works ok, but its not the correct way to do it.
George
.NET ProgrammingC#ASP.NET

Avatar of undefined
Last Comment
EricFleet
Avatar of EricFleet
EricFleet

Include the script in the aspx page not the cs.
ASKER CERTIFIED SOLUTION
Avatar of EricFleet
EricFleet

Blurred text
THIS SOLUTION IS ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
I would use:
            Literal1.Text = string.Format(@"
                <script src=""http://maps.google.com/maps?file=api&amp;v=2&amp;key=****
                type=""text/javascript""></script>
                <script type=""text/javascript"">
                //<![CDATA[
                function load() {
                    if (GBrowserIsCompatible()) {
                        var map = new GMap2(document.getElementById(""map""));
                        map.setMapType(G_NORMAL_MAP);
                        map.setCenter(new GLatLng({0},{1}), 5);
                        map.addControl(new GSmallMapControl());
                        map.addControl(new GMapTypeControl());
                        map.openInfoWindow(map.getCenter(), document.createTextNode(""City""))
                    }
                }
                //]]>
                </script>", lat, lon);

Open in new window

Notice in my code:
- I used @ prefix to cancel the effect of the backslash special character, and allow newlines in strings
- I used String.Format and placed {0} and {1} to specify lat/lon in next arguments
- Quotes are replaces with double quotes ("") instead of \"
Avatar of BeginningWebDesign

ASKER

Hi Jaime
I get the following error:

Input string was not in a correct format

George
Avatar of EricFleet
EricFleet

jaime,

Out of curiosity, why would you go with the literal instead of  RegisterStartupScript?
Avatar of BeginningWebDesign

ASKER

Hi EricFleet:

I'm trying both, currently working on yours

George
Avatar of BeginningWebDesign

ASKER

Hi EricFleet

I'm currently reading up on RegisterStartupScript as never heard of that before, currently have:

string scriptKey = "GoogleEarth";
            if (!ClientScript.IsStartupScriptRegistered(scriptKey))
            {
                StringBuilder GoogleJavascript = new StringBuilder();
                GoogleJavascript.Append("<script language=JavaScript>\n");
                GoogleJavascript.Append("</script>\n");
                ClientScript.RegisterStartupScript(Type.Missing.GetType(), scriptKey, GoogleJavascript.ToString());
            }

And seems to be working ok for now

George
Sorry, the braces were causing problems. Correct way is:

Literal1.Text = string.Format(@"
                <script src=""http://maps.google.com/maps?file=api&v=2&key=****
                type=""text/javascript""></script>
                <script type=""text/javascript"">
                //<![CDATA[
                function load() {{
                    if (GBrowserIsCompatible()) {{
                        var map = new GMap2(document.getElementById(""map""));
                        map.setMapType(G_NORMAL_MAP);
                        map.setCenter(new GLatLng({0},{1}), 5);
                        map.addControl(new GSmallMapControl());
                        map.addControl(new GMapTypeControl());
                        map.openInfoWindow(map.getCenter(), document.createTextNode(""City""))
                    }}
                }}
                //]]>
                </script>", lat, lon);
Avatar of BeginningWebDesign

ASKER

Did some reading up and found RegisterStartupScript to be most suitable, one point Page.RegisterStartupScript is now obsolete, its now ClientScript.RegisterStartupScript
http://msdn.microsoft.com/en-us/library/system.web.ui.page.registerstartupscript.aspx
George
Avatar of EricFleet
EricFleet

Per the original poster: "Did some reading up and found RegisterStartupScript to be most suitable, one point Page.RegisterStartupScript is now obsolete, its now ClientScript.RegisterStartupScript"
http://msdn.microsoft.com/en-us/library/system.web.ui.page.registerstartupscript.aspx"

I was not aware of this and figured that this might be nice for anyone who comes across this answer down the road.
.NET Programming
.NET Programming

The .NET Framework is not specific to any one programming language; rather, it includes a library of functions that allows developers to rapidly build applications. Several supported languages include C#, VB.NET, C++ or ASP.NET.

137K
Questions
--
Followers
--
Top Experts
Get a personalized solution from industry experts
Ask the experts
Read over 600 more reviews

TRUSTED BY

IBM logoIntel logoMicrosoft logoUbisoft logoSAP logo
Qualcomm logoCitrix Systems logoWorkday logoErnst & Young logo
High performer badgeUsers love us badge
LinkedIn logoFacebook logoX logoInstagram logoTikTok logoYouTube logo