简体   繁体   中英

how to cal javascript method in server side button control?

  function calcRoute() {
        var start = document.getElementById('start').value;
        var end = document.getElementById('end').value;


        var request = {
            origin: start,
            destination: end,

            travelMode: google.maps.TravelMode.DRIVING
        };
        directionsService.route(request, function(response, status) {
            if (status == google.maps.DirectionsStatus.OK) {
                directionsDisplay.setDirections(response);
                var route = response.routes[0];
                var summaryPanel = document.getElementById('directions_panel');
                summaryPanel.innerHTML = '';
                // For each route, display summary information.
                for (var i = 0; i < route.legs.length; i++) {
                    var routeSegment = i + 1;
                    summaryPanel.innerHTML += '<b>Route Segment: ' + routeSegment + '</b><br>';
                    summaryPanel.innerHTML += route.legs[i].start_address + ' to ';
                    summaryPanel.innerHTML += route.legs[i].end_address + '<br>';
                    summaryPanel.innerHTML += route.legs[i].distance.text + '<br><br>';
                }
            }
        });
    }

    google.maps.event.addDomListener(window, 'load', initialize);

my button event is not fired...i want to shows the get directions using two dropdown list controls using server side controls but my button event is not fired ...wht i do?

 <asp:Button ID="btnsubmit" runat="server" OnClientClick="return calcRoute();" Text="Submit" />

how to cal javascript method into serverside...please tell me..

Why have you got a server side button in the first place?

The button doesn't call a submit event, nor does it post back to the server. So why not simply use a normal html button?

Even your Javascript function doesn't return a true or a false so using return calcRoute() will always return a false event.

In any case, try using the normal onClick event rather than OnClientClick.

edit

Seems to me a lot of what you are doing here could be solved using jQuery in a much better way than you are trying to do here.

Syntax : 
public void RegisterClientScriptBlock(
    Type type,
    string key,
    string script,
    bool addScriptTags
)

Parameters

type
    Type: System.Type

    The type of the client script to register. 

key
    Type: System.String

    The key of the client script to register. 

script
    Type: System.String

    The client script literal to register. 

addScriptTags
    Type: System.Boolean

    A Boolean value indicating whether to add script tags.

  public void Page_Load(Object sender, EventArgs e)
  {
    // Define the name and type of the client scripts on the page.
    String csname1 = "PopupScript";
    String csname2 = "ButtonClickScript";
    Type cstype = this.GetType();

    // Get a ClientScriptManager reference from the Page class.
    ClientScriptManager cs = Page.ClientScript;

    // Check to see if the startup script is already registered.
    if (!cs.IsStartupScriptRegistered(cstype, csname1))
    {
      String cstext1 = "alert('Hello World');";
      cs.RegisterStartupScript(cstype, csname1, cstext1, true);
    }

    // Check to see if the client script is already registered.
    if (!cs.IsClientScriptBlockRegistered(cstype, csname2))
    {
      StringBuilder cstext2 = new StringBuilder();
      cstext2.Append("<script type=\"text/javascript\"> function DoClick() {");
      cstext2.Append("Form1.Message.value='Text from client script.'} </");
      cstext2.Append("script>");
      cs.RegisterClientScriptBlock(cstype, csname2, cstext2.ToString(), false);
    }
  }

I am not add "return" false condition...My Button is getting a PostBack right now, that could be a problem. So add

Return false;

condition..

Then it will be working fine...thanks for the response..

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