0

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

aliiii
  • 3
  • 1
  • 4
  • possible duplicate of [How to call a code behinds button click event using javascript](http://stackoverflow.com/questions/14156327/how-to-call-a-code-behinds-button-click-event-using-javascript) – krillgar Feb 25 '15 at 17:54

1 Answers1

0

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

Community
  • 1
  • 1
adricadar
  • 9,971
  • 5
  • 33
  • 46
  • btn_newpl just defined in javascript , i dont have access this button in html and i can't do this ! – aliiii Feb 25 '15 at 18:25
  • @aliiii your button don't have 1 important attribute runat="server" an the OnClick event must be changed in OnServerClick – adricadar Feb 25 '15 at 19:11
  • when i use OnServerClick="btn_newpl_Click" and runat="server" together map not loading !! Note that my button is in script tag not in html – aliiii Feb 26 '15 at 04:55
  • @aliiii try to add input element on page and get it with javascript, as an workaround on this, see my updated answer. – adricadar Feb 26 '15 at 11:00
  • @aliiii please fell free to edit my answer, if you think changes that you made can improve it :) – adricadar Feb 26 '15 at 18:09
  • Most of the changes was in C # :-) Anyway, i changed this answer a little – aliiii Feb 27 '15 at 07:40
  • can i use this method to create textbox ? – aliiii Feb 27 '15 at 13:44
  • Yes, you can use this method for almost all controls (or even all :D) – adricadar Feb 28 '15 at 18:37
  • to create textbox , i use this codes ! html code : and javascript code : var txt_java = document.createElement("input"); txt_java.appendChild(document.getElementById('txt_newpl')); how to access value of this textbox ? txt_java.value not working – aliiii Mar 01 '15 at 08:54
  • @aliiii You are doing it wrong, but this is another question. Also you should consider to rename this question. – adricadar Mar 01 '15 at 09:27
  • ok :-) http://stackoverflow.com/questions/28792246/get-value-of-dom-element-in-code-behind Can you help me? – aliiii Mar 01 '15 at 10:06