2014-12-01 3 views
1

Это код для вызова функции яваскрипта из asp.net кода JavaScript:Asp.net код не вызывать функцию

ASPX Файл:

<%@ Page Language="C#" MasterPageFile="~/MasterPage.Master" AutoEventWireup="true" CodeBehind="Restaurants.aspx.cs" Inherits="WebApplication1.WebForm1" %> 
<asp:Content ID="Content1" ContentPlaceHolderID="head" runat="Server"> 
    <script type="text/javascript"> 
     function alertMe() { 
      alert('Hello'); 
     } 
    </script> 
</asp:Content> 
<asp:Content ID="Content2" ContentPlaceHolderID="body" runat="Server"> 
</asp:Content> 

aspx.cs Файл:

 protected void Page_Load(object sender, EventArgs e) 
      { 
       if (!Page.IsPostBack) 
       { 
        Page.ClientScript.RegisterStartupScript(this.GetType(), "CallMyFunction","alertMe()", true); 
       } 
       } 

Проблема: код asp.net не удается вызвать функцию яваскрипта I Searc Hed для решения несколько ссылок являются: http://www.dotnetcurry.com/showarticle.aspx?ID=273 Calling JavaScript Function From CodeBehind http://www.devcurry.com/2009/01/execute-javascript-function-from-aspnet.html

, но не смогли найти решение. Может кто-нибудь мне помочь.

PAGE ИСТОЧНИК

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 
<html lang="en"> 
<head> 
    <meta charset="utf-8"> 
    <meta http-equiv="X-UA-Compatible" content="IE=edge"> 
    <meta name="viewport" content="width=device-width, initial-scale=1"> 
    <meta name="description" content=""> 
    <meta name="author" content=""> 
    <meta http-equiv="content-type" content="text/html; charset=UTF-8" /> 
    <!-- Bootstrap --> 
    <link href="../css/bootstrap.min.css" rel="stylesheet"> 
    <link href="css/heroic-features.css" rel="stylesheet"> 
    <link href="../css/jasny-bootstrap.min.css" rel="stylesheet" media="screen"> 
    <link rel="stylesheet" href="../css/bootstrapValidator.min.css" /> 
    <link rel="stylesheet" href="../css/dist/bootstrapValidator.min.css" /> 
    <link type="text/css" rel="stylesheet" href="https://fonts.googleapis.com/css?family=Roboto:300,400,500"> 
    <!-- HTML5 Shim and Respond.js IE8 support of HTML5 elements and media queries --> 
    <!-- WARNING: Respond.js doesn't work if you view the page via file:// --> 
    <!--[if lt IE 9]> 
     <script src="https://oss.maxcdn.com/html5shiv/3.7.2/html5shiv.min.js"></script> 
     <script src="https://oss.maxcdn.com/respond/1.4.2/respond.min.js"></script> 
    <![endif]--> 
    <!-- jQuery (necessary for Bootstrap's JavaScript plugins) 
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>--> 
    <!-- jQuery Version 1.11.0 --> 
    <script src="js/jquery-1.11.0.js"></script> 
    <!-- Include all compiled plugins (below), or include individual files as needed --> 
    <script src="js/bootstrap.min.js"></script> 
    <script src="js/jasny-bootstrap.min.js"></script> 
    <script type="text/javascript" src="js/bootstrapValidator.js"></script> 
    <script type="text/javascript" src="../js/dist/bootstrapValidator.min.js"></script> 
    <link type="text/css" rel="stylesheet" href="https://fonts.googleapis.com/css?family=Roboto:300,400,500"> 
    <script src="https://maps.googleapis.com/maps/api/js?v=3.exp&libraries=places"></script> 
    <script> 
     // This example displays an address form, using the autocomplete feature 
     // of the Google Places API to help users fill in the information. 

     var placeSearch, autocomplete; 
     var componentForm = { 
      street_number: 'short_name', 
      route: 'long_name', 
      locality: 'long_name', 
      administrative_area_level_1: 'short_name', 
      country: 'long_name', 
      postal_code: 'short_name' 
     }; 

     function initialize() { 
      // Create the autocomplete object, restricting the search 
      // to geographical location types. 
      autocomplete = new google.maps.places.Autocomplete(
      /** @type {HTMLInputElement} */(document.getElementById('autocomplete')), 
     { types: ['geocode'] }); 
      // When the user selects an address from the dropdown, 
      // populate the address fields in the form. 
      google.maps.event.addListener(autocomplete, 'place_changed', function() { 
       fillInAddress(); 
      }); 
     } 

     // [START region_fillform] 
     function fillInAddress() { 
      // Get the place details from the autocomplete object. 
      var place = autocomplete.getPlace(); 

      for (var component in componentForm) { 
       document.getElementById(component).value = ''; 
       document.getElementById(component).disabled = false; 
      } 

      // Get each component of the address from the place details 
      // and fill the corresponding field on the form. 
      for (var i = 0; i < place.address_components.length; i++) { 
       var addressType = place.address_components[i].types[0]; 
       if (componentForm[addressType]) { 
        var val = place.address_components[i][componentForm[addressType]]; 
        document.getElementById(addressType).value = val; 
       } 
      } 
     } 
     // [END region_fillform] 

     // [START region_geolocation] 
     // Bias the autocomplete object to the user's geographical location, 
     // as supplied by the browser's 'navigator.geolocation' object. 
     function geolocate() { 
      if (navigator.geolocation) { 
       navigator.geolocation.getCurrentPosition(function (position) { 
        var geolocation = new google.maps.LatLng(
      position.coords.latitude, position.coords.longitude); 
        autocomplete.setBounds(new google.maps.LatLngBounds(geolocation, 
      geolocation)); 
       }); 
      } 
     } 
     // [END region_geolocation] 

    </script> 





    <script type="text/javascript"> 
     function alertMe() { 
      alert('Hello'); 
     } 
    </script> 


</head> 
<body onload="initialize()"> 
    <!-- Navigation --> 
    <nav class="navbar navbar-inverse navbar-fixed-top" role="navigation"> 
     <div class="container"> 
      <!-- Brand and toggle get grouped for better mobile display --> 
      <div class="navbar-header"> 
       <button type="button" class="navbar-toggle" data-toggle="collapse" data-target="#bs-example-navbar-collapse-1"> 
        <span class="sr-only">Toggle navigation</span> 
        <span class="icon-bar"></span> 
        <span class="icon-bar"></span> 
        <span class="icon-bar"></span> 
       </button> 
       <a class="navbar-brand" href="Home.aspx">Home</a> 
      </div> 
      <!-- Collect the nav links, forms, and other content for toggling --> 
      <div class="collapse navbar-collapse" id="bs-example-navbar-collapse-1"> 
       <ul class="nav navbar-nav"> 
        <li> 
         <a href="#">About</a> 
        </li> 
        <li> 
         <a href="#">Contact</a> 
        </li> 
       </ul> 
      </div> 
      <!-- /.navbar-collapse --> 
     </div> 
     <!-- /.container --> 
    </nav> 
    <!-- Page Content --> 
    <div class="container"> 

    <!-- Page Content --> 


     <hr> 
    </div> 
    <!-- /.container --> 
</body> 
</html> 
+1

«Код asp.net не может вызвать функцию javascript» - что это значит? Вы посмотрели страницу html страницы браузера? Вы нашли определение «alertMe» и позвонили туда? – Igor

+0

Nope 'alertMe' не вызвал код asp.net. –

+1

Не могли бы вы показать нам всю html-страницу (вы можете использовать 'View page source')? – Mivaweb

ответ

1

Im точно не знаю, как назвать это из кода CS, но не могли бы вы положили что-то вроде это Ваша страница ASPX?

<script type="text/javascript"> 
    function alertMe() { 
     alert('Hello'); 
    } 

var isPostBack = '<%=Page.IsPostBack%>' == 'True' ? true : false; 

if (!isPostBack){ 
    alertMe(); 
} 
</script> 
Смежные вопросы