简体   繁体   中英

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

I'm doing a school project, I need to make a simple web site, add google maps on it, read, lets say, 100 diffrent addresses from a text file and show those locations on google maps with markers.

Now I'm trying to add google maps to my ASP.net page with javascript which I saw on google maps tutorials. And there's that problem which I have to convert adresses to coordinates. So for that I'm using

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

these functions and they are working fine. But my problem here is, I need to get these address informations from a text file. But to get them, I need to use a few diffrent codes. And I want to make it on the server side with C# codes. But I don't know how to write some codes on server side and then return something to HTML side as address. I hope you understand. Thank you for your helps.

Update:

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

Now if I use "asa" instead of Skills[0] it will show the location and mark, but with Skills[0] it's not working. And thank you for your answer that was what I'm looking for.

even if I try var MyValue = Skills[0]; and then use MyValue instead of Skills[0] it's still not working

If I understood your question correctly, you want to create an array on the server side and read it in the client.

See this link for a tutorial on how to pass an array from the server to the client.

Basically, you want to use the ClientScriptManager.RegisterArrayDeclaration method to add values to the array.

You can then easily read it in javascript.

Server Side:

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

Javascript on Client Side:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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