开发者

Problem with ASP.NET when using IsPostback + Properties + RegisterStartupScript + Google Maps API

After I successfully created my first google maps in asp.net with this question 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.31开发者_如何学Go6923";
        }


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


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

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜