2015-05-07 100 views
1

我写这篇文章的代码为我的网页:
如何用asp.net按钮调用java脚本函数?

<script> 
    function initialize() { 
     var mapProp = { 
      center: new google.maps.LatLng(51.508742, -0.120850), 
      zoom: 7, 
      mapTypeId: google.maps.MapTypeId.ROADMAP 
     }; 
     var map = new google.maps.Map(document.getElementById("googleMap"), mapProp); 
    } 

    function loadScript() { 
     var script = document.createElement("script"); 
     script.type = "text/javascript"; 
     script.src = "http://maps.googleapis.com/maps/api/js?key=&sensor=false&callback=initialize"; 
     document.body.appendChild(script); 
    } 

    window.onload = loadScript; 
</script> 

我希望在asp按钮click初始化功能,并设置该行的新值:

center: new google.maps.LatLng(51.508742--->Load from asp:Text1, -0.120850--->Load from asp:Text2) 

我怎么能做这个?

回答

1

你可以从ASP按钮事件

private void Button_clicked(object s, EventArgs e) 
{ 
    //your code... 

    Page.ClientScript.RegisterStartupScript(this.GetType(), "calljs", "initialize();", true); 

    // 'calljs' is just a key, can be anything 
    // to pass values 
    Page.ClientScript.RegisterStartupScript(this.GetType(), "calljs", "initialize(" + val1 + ", " + val2 + ");", true); 
    // and modify your js function to allow parameters 
} 

调用JavaScript函数,如果你想打电话initialize();按钮触发的事件之前,

添加onclientclick="initialize();"属性为ASP按钮

+0

什么是calljs?我怎样才能发送新的价值从asp文本框功能? –

+0

我的朋友,我想要当我点燃按钮发送文本框值为JavaScript初始化()函数。 –

+0

检查updatd – Jag