views:

23

answers:

1

After I successfully created my first google maps in asp.net with this question http://stackoverflow.com/questions/3897781/google-maps-api-in-asp-net-masterpage-why-it-stays-blank

I now want to change the maps lat, long when clicking with Go button. For this I just need to pass a new x,y at startupsctipt to GoogleMaps js function.

first load is ok, but it's no more OK on second load maps is empty, I can't see why:

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


    private String m_x;
    private String m_y;

    protected void Page_Load(object sender, EventArgs e)
    {
        String x;
        String y;

        if (!IsPostBack)
        {

            m_x = "48.854401";
            m_y = "2.316923";
        }


        String script = "GoogleMaps('" + m_x + "," + m_y + ")";
        ClientScript.RegisterStartupScript(this.GetType(), "GoogleMaps", script, true);
    }

    protected void ButtonGo_Click(object sender, EventArgs e)
    {
        m_x = "-34.397";
        m_y = "150.644";
    }
}
+1  A: 

In your code, m_x and m_y are set only on the first page load (if(!IsPostBack)), if you want to keep the values, save them in viewstate for example.

The event Page_Load happens before the Button_Click event. So the javascript is written before you set the values in the Button_Click. See this page for the order the events happen: http://msdn.microsoft.com/en-us/library/ms178472.aspx

GôTô
Sorry but I don't understand your remark: did you see they are also set in ButtonGo_Click ? that's where I want them to be taken into account when I click on this button.
@user310291 Goto have right, you use the m_x and m_y with out have set it first, I wondering why is not thow you an error.
Aristos
@Aristos I made a debug and m_x contains null. OK thanks Goto I'll have to call from button too.