locked
calling Bing Maps Javascript function from code behind RRS feed

  • Question

  • I'm trying to execute Javascript from code behind. Here is the sample of what I'm trying to do..

    Protected Sub btnSave_Click(ByVal sender As Object, ByVal e As EventArgs) Handles btnSave.Click

            Dim objClient As New cls_Client

            objClient.FirstName = txtFName.Text
            objClient.LastName = txtLName.Text
            objClient.MI = txtMI.Text

            objClient.PrimDesp = txtPrimDesp.Text
            objClient.PrimAddr1 = txtPrimAddr1.Text
            objClient.PrimAddr2 = txtPrimAddr2.Text

    // few more lines of code

    //call getAddr()

    // and  few more lines

    End Sub

     

    <script type="text/javascript" >

    function getAddr()    {
                 myMap.Clear();  
       
                            var  primAddr1 = document.getElementById('<%=txtPrimAddr1.ClientID%>').value;

    //few more lines

    address= primAddr1 +", " + primAddr2;

    startGeocoding(address);

    }

    I've used RegisterStartUpScript to call the function..

    If Not ClientScript.IsStartupScriptRegistered("testing") Then
                Page.ClientScript.RegisterStartupScript(Me.GetType, "testing", "getAddr();", True)
            End If

    I'm able to call javascript function in Page_Load but not in btnSave_Click handler...
    Does this got to do anything with the PostBack???
    Friday, March 12, 2010 5:59 PM

Answers

  • The asp.net button has a client click event where you can call the javascript method directly:

    <asp:Button runat="server" OnClientClick="javascript:getAddr();" />

    Also rather then set the parameter in that textbox you can pass it directly in this method.


    Windows Live Developer MVP - www.soulsolutions.com.au - follow http://twitter.com/bingmapsdev for latest news.
    Wednesday, March 2, 2011 6:12 AM
    Moderator

All replies

  • If you are using AJAX/Update panels then you should use the ScriptManager version of that method.
    See the documentation here:
    http://msdn.microsoft.com/en-us/library/bb310408.aspx
    Friday, March 12, 2010 6:33 PM
  • Thanks Sasha..I'm not using Ajax/Update panels...

    my problem is I'm able to call the function in Page_Load but on in the BtnSave_Click...
    Friday, March 12, 2010 7:31 PM
  • The asp.net button has a client click event where you can call the javascript method directly:

    <asp:Button runat="server" OnClientClick="javascript:getAddr();" />

    Also rather then set the parameter in that textbox you can pass it directly in this method.


    Windows Live Developer MVP - www.soulsolutions.com.au - follow http://twitter.com/bingmapsdev for latest news.
    Wednesday, March 2, 2011 6:12 AM
    Moderator