Resgistering javascript in a usercontrol
I am trying to use google maps and for that I have made a usercontrol where I am trying to register a javascript, but again like always it did not get registered, so I tried to register it code behind using clientscript. But again, the same problem. It does not gets registered.
The following code is written in the page load event of the usercontrol.
ClientScriptManager script = Page.ClientScript;
if (!script.IsStartupScriptRegistered(this.GetType(), "scriptKey"))
{
StringBuilder sb = new StringBuilder();
sb.AppendLine("function initialize() {");
sb.AppendLine("var latlng = new google.maps.LatLng(%%LAT%%, %%LONG%%);");
sb.AppendLine("var myOptions = {");
sb.AppendLine("zoom: 8,");
sb.AppendLine("center: latlng,");
sb.AppendLine("mapTypeId: google.maps.MapTypeId.ROADMAP");
sb.AppendLine("};");
sb.AppendLine("var map = new google.maps.Map(document.getElementById(\"\"%%CLIENTID%%\"\"), myOptions);");
sb.AppendLine("}");
sb = sb.Replace("%%CLIENTID%%", this.ClientID);
sb = sb.Replace("%%LAT%%", (a1 == null ? 42.006160736084 : a1.latitude).ToString());
sb = sb.Replace("%%LONG%%", (a1 == null ? -开发者_如何转开发93.6386795043945 : a1.longitude).ToString());
script.RegisterStartupScript(this.GetType(), "scriptKey", sb.ToString(), false);
and then I try to use this control in an aspx page. Any help would be appreciated, Thanks!
You forgot the tags.
StringBuilder sb = new StringBuilder();
sb.AppendLine("<script type=\"text/javascript\"> function initialize() {");
......
......
......
......
sb = sb.Replace("%%LAT%%", (a1 == null ? 42.006160736084 : a1.latitude).ToString());
sb = sb.Replace("%%LONG%%", (a1 == null ? -93.6386795043945 : a1.longitude).ToString());
sb.Append("</script>");
精彩评论