0

我正在尝试使用谷歌地图,为此我制作了一个用户控件,我试图在其中注册一个 javascript,但再次像往常一样它没有被注册,所以我尝试使用客户端脚本在后面注册它的代码。但同样的问题。它没有被注册。

下面的代码写在用户控件的页面加载事件中。

 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);

然后我尝试在 aspx 页面中使用此控件。任何帮助将不胜感激,谢谢!

4

1 回答 1

2

你忘记了标签。

        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>");
于 2011-01-26T22:42:58.130 回答