微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

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

我正在做一个学校项目,我需要建立一个简单的网站,在其上添加谷歌地图,从文本文件中读取,比如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]它仍然无法正常工作

解决方法

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

有关如何将数组从服务器传递到客户端的教程,请参见this link.

基本上,您希望使用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];
            }
 }

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。

相关推荐