i’m having a problem where I am trying to save the geolocation of an address unto a file. I am using hidden labels to transfer the information.
on the client side button event I have:
function save() {
document.getElementById("hidLat").value = y;
document.getElementById("hidLon").value = x;
<% saveAddress s = save(); %>
}
and in c# I have:
protected saveAddress save()
{
saveAddress s = new saveAddress();
s.latitude = hidLat.Value;
s.longitude = hidLon.Value;
using (System.IO.StreamWriter file = new System.IO.StreamWriter(@"C:\\savedAddresses.txt", true))
{
file.WriteLine(s.latitude + " " + s.longitude);
file.Close();
}
return s;
}
When I click on the button the file is created but nothing is saved into it.
Am I doing this right or is there a better way to try and write the user input into a file?
I think you’re getting confused on the web lifecycle. In your particular instance, this is what is happening:
In the process of executing your page, the ASPX contains code to call
In this way, that method is called before the page is even rendered back to the client.
When the page is finally sent down to the client, you have some client side code invoking
function save().Im guessing you want that server side method to be invoked after the javascript function has run. You have two options:
Page.IsPostBackand attempt to call your server side method.savemethod.