gpt4 book ai didi

javascript - 如何使用asp.net按钮调用javascript函数?

转载 作者:行者123 更新时间:2023-11-28 00:20:05 25 4
gpt4 key购买 nike

我正在为我的页面编写以下代码:

<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按钮单击初始化函数时,并为此行设置新值:

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

我该怎么做?

最佳答案

您可以从 asp 按钮事件调用 javascript 函数

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
}

如果您想在按钮事件触发之前调用initialize();

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

关于javascript - 如何使用asp.net按钮调用javascript函数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30106505/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com