繁体   English   中英

如何使用C#从文件中读取内容并在ASP.NET页面上将其与HTML代码一起使用?

[英]How to read something from a file with C# and use it with HTML codes on ASP.NET page?

我正在做一个学校项目,我需要建立一个简单的网站,在其上添加谷歌地图,从文本文件中读取,比如100个不同的地址,并在谷歌地图上显示带标记的那些位置。

现在我正在尝试使用javascript将谷歌地图添加到我的ASP.net页面,这是我在谷歌地图教程中看到的。 而且我必须将地址转换为坐标。 所以我正在使用它

function addAddressToMap(response) {
    if (!response || response.Status.code != 200) {
        alert("Sorry, we were unable to geocode that address");
    } 
    else {
        place = response.Placemark[0];
        point = new GLatLng(place.Point.coordinates[1],place.Point.coordinates[0]);
        marker = new GMarker(point);
        map.addOverlay(marker);
        marker.openInfoWindowHtml(place.address + '<br>' + '<b>Country code:</b> ' + place.AddressDetails.Country.CountryNameCode);
    }
}

// showLocation() is called when you click on the Search button
// in the form.  It geocodes the address entered into the form
// and adds a marker to the map at that location.
function showLocation() {
    var address = "izmit";
    var address2 = "ağrı";
    geocoder.getLocations(address, addAddressToMap);
    geocoder.getLocations(address2, addAddressToMap);
}

这些功能,他们工作正常。 但我的问题是,我需要从文本文件中获取这些地址信息。 但要获得它们,我需要使用一些不同的代码。 我想用C#代码在服务器端制作它。 但我不知道如何在服务器端编写一些代码,然后将某些内容返回到HTML端作为地址。 我希望你明白。 谢谢你的帮助。

更新:

Server code:

public partial class _Default : System.Web.UI.Page
{

    protected void Page_Load(object sender, EventArgs e)
    {

        Page.ClientScript.RegisterArrayDeclaration("Skills", "'asa'");
        Page.ClientScript.RegisterArrayDeclaration("Skills", "'bell'");
        Page.ClientScript.RegisterArrayDeclaration("Skills", "'C'");
        Page.ClientScript.RegisterArrayDeclaration("Skills", "'C++'");
    }
}

Client side:

function showLocation()
{
    var address = "izmit";
    var address2 = "ağrı";
    geocoder.getLocations(Skills[0], addAddressToMap);
}

现在,如果我使用“asa”而不是Skills [0],它将显示位置和标记,但是使用Skills [0]它不起作用。 谢谢你的回答,这正是我正在寻找的。

即使我尝试var MyValue = Skills [0]; 然后使用MyValue而不是Skills [0]它仍然无法正常工作

如果我正确理解了您的问题,您希望在服务器端创建一个数组并在客户端中读取它。

有关如何将数组从服务器传递到客户端的教程,请参阅此链接

基本上,您希望使用ClientScriptManager.RegisterArrayDeclaration方法向数组添加值。

然后,您可以在javascript中轻松阅读它。

服务器端:

string arrayName = "MyArray";
Page.ClientScript.RegisterArrayDeclaration(arrayName , "'value1'");
Page.ClientScript.RegisterArrayDeclaration(arrayName , "'value2'");

客户端的Javascript:

function readArray() 
{
            for (var i = 0; i < MyArray.length; i++) 
            {
                //Reading Element From Array
                var myValue = MyArray[i];
            }
 }

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM