2014-12-01 2 views
1

이 asp.net 코드에서 자바 스크립트 함수를 호출하는 코드를 호출 실패Asp.net 코드가 자바 스크립트 함수

영문 파일 : 파일 aspx.cs

<%@ 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> 

:

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

문제 : asp.net 코드는 자바 스크립트 함수 I의 공학자를 호출 실패 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

하지만 해결책을 찾을 수 없습니다 :을 Hed 솔루션에 대한 몇 가지 링크가 있습니다. 누구든지 나를 도와 줄 수 있어요.

페이지 소스

<!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 코드가 자바 스크립트 함수를 호출하지 못했습니다."- 이것은 무엇을 의미합니까? 브라우저의 페이지 소스 html을 보셨습니까? 'alertMe' 정의를 찾았습니까? – Igor

+0

아니요'alertMe'는 asp.net 코드로 호출하지 않았습니다. –

+1

전체 HTML 페이지를 보여줄 수 있습니까? ('페이지 소스보기 '를 사용할 수 있습니까?) – Mivaweb

답변

1

임 정확히 확인하는 방법 CS 코드에서 호출 할 수 있지만, 이런 일이 영문 페이지입니다 골로 연결되지 못했습니다?

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

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

if (!isPostBack){ 
    alertMe(); 
} 
</script> 
관련 문제