简体   繁体   English

从javascript onclick事件按钮调用c#函数

[英]call c# function from javascript onclick event button

how to use something like this : 如何使用这样的东西:

<script>
 google.maps.event.addListener(marker, "click", function (e) {
     var infoWindow = new google.maps.InfoWindow({
content: 
'<div><input type="text" id="txt_newpl" value="place name"/>' +
'</br> <input type="button" id="btn_newpl" value="submit" onclick="btn_newpl_Click" /></div>'
         });</script>

and c# code is like this 和c#代码是这样的

    protected void btn_newpl_Click(object sender, EventArgs e)
{...}

how can call this c# function from javascript event click 如何从javascript事件点击调用此c#函数

To trigger button event, you have to add this line of code in javascript. 要触发按钮事件,您必须在javascript中添加此行代码。

document.getElementById("btn_newpl").click();

Your button must have runat="server" and OnServerClick="btn_newpl_Click" attributes. 您的按钮必须具有runat="server"OnServerClick="btn_newpl_Click"属性。

If your problem is that the input element is in javascript string , than you can extract the string value of input and concatenate to your string on client side . 如果您的问题是input元素是javascript字符串 ,那么您可以提取 input 的字符串值并连接到客户端的字符串。

<input type="button" id="btn_newpl" value="submit" 
       OnServerClick="btn_newpl_Click"
       runat="server" />

<script>
var tmp = document.createElement("div");
tmp.appendChild(document.getElementById('btn_newpl'));

google.maps.event.addListener(marker, "click", function (e) {
    var infoWindow = new google.maps.InfoWindow({
       content: 
       '<div><input type="text" id="txt_newpl" value="place name"/>' +
       '</br>'+tmp.innerHTML+'</div>'
    })
});
</script>

Please note that you don't actually call a C# function , you just trigger the click event on webforms and a postback to sever will be done. 请注意,您实际上并没有 调用C#函数 ,只需触发webforms上的click事件,就会完成对服务器回发 The server will figure out which event has been triggered and will execute the C# function. 服务器将确定哪个事件已被触发并将执行C#功能。

Source: How to call a code behinds button click event using javascript 来源: 如何使用javascript调用代码隐藏按钮单击事件

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 在C#中的按钮OnClick事件之间调用javascript - Call javascript in between a button OnClick event in C# 使用按钮 OnClick 事件调用 Javascript 函数 - Call Javascript function using button OnClick event Javascript 按钮 onclick 事件调用 function 内部 ZC1C425268E68385D14AB5074C17A 模块 - Javascript button onclick event to call function inside function in module 在c#Button事件中一起调用OnClientClick和Onclick - call OnClientClick and Onclick together in c# Button Event 如何从Perl中的Submit按钮的onclick事件中调用Javascript函数? - How do I call a Javascript Function from onclick event of submit button in Perl? 无法从按钮onclick事件ASP.NET 4调用Javascript函数 - Cant call Javascript function from button onclick event ASP.NET 4 在ASP.NET链接按钮上,是否可以将JavaScript函数调用从“ href”属性移至onclick事件? - On an ASP.NET link button, Is it possible to move the JavaScript function call from the “href” attribute to the onclick event? Blazor 一键执行 C# 和 JS function ZC0BB2196426022E85ADF9A5B6D34FD事件 - Blazor execute C# and JS function in one button onclick event 动态添加按钮时,从onclick事件调用原型函数 - Call prototype function from onclick event, when button is added dynamically C#-调用javascript函数onclick并传递字符串 - C# - call javascript function onclick and passing string
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM