简体   繁体   中英

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

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

how can call this c# function from javascript event click

To trigger button event, you have to add this line of code in javascript.

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

Your button must have runat="server" and OnServerClick="btn_newpl_Click" attributes.

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 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. The server will figure out which event has been triggered and will execute the C# function.

Source: How to call a code behinds button click event using javascript

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM