3

I have two labels:

  <asp:Label ID="Label1" runat="server" Text="Label"></asp:Label>
  <asp:Label ID="Label2" runat="server" Text="Label"></asp:Label>

and I set innerHTML by javascript:

document.getElementById('Label1').innerHTML = position.lat();
document.getElementById('Label2').innerHTML = position.lng();

How I can get those labels values in codebehind? I try:

TextBox2.Text = Label1.Text;

UPDATE:I need to get pushpin location:

  <artem:GoogleMap ID="GoogleMap1" runat="server" 
    EnableMapTypeControl="False" MapType="Roadmap" >        
  </artem:GoogleMap>      
  <artem:GoogleMarkers ID="GoogleMarkers1" runat="server" 
    TargetControlID="GoogleMap1" onclientpositionchanged="handlePositionChanged">
  </artem:GoogleMarkers> 
  <asp:Label ID="Label1" runat="server" Text="Label"></asp:Label>
  <asp:Label ID="Label2" runat="server" Text="Label"></asp:Label>

<script type="text/javascript">
  var list = document.getElementById("Label1");
  function handlePositionChanged(sender, e) {
    printEvent("Position Changed", sender, e);
  }
  function printEvent(name, sender, e) {
    var position = e.latLng || sender.markers[e.index].getPosition();
    document.getElementById('Label1').innerHTML = position.lat();
    document.getElementById('Label2').innerHTML = position.lng();
  }
</script>

protected void Button1_Click(object sender, EventArgs e)
{
    TextBox2.Text = Label1.Text;// return value: Label
}
4
  • yes, your code seems to be correct. just confirm the values before assigning Commented Aug 31, 2012 at 14:48
  • 2
    No you cannot. To transfer data from client to server use input controls or Ajax. Commented Aug 31, 2012 at 14:49
  • What exactly is not working with your code? You should post a full example of what you tried and what happened so we can help you. Commented Aug 31, 2012 at 14:50
  • To have the updated data available to the page's code behind, you have to have the page post back first. Commented Aug 31, 2012 at 14:59

3 Answers 3

6

You cannot access the value on server side. You will have to use a hidden field for that:

<asp:HiddenField ID="Hidden1" runat="server" />

The set the innerHtml value in the Hidden field by doing:

document.getElementById('<%= Hidden1.ClientID %>').value = position.lat();

You can then access it from server side by doing:

TextBox1.Text = Hidden1.Value;
Sign up to request clarification or add additional context in comments.

Comments

1

You are not able to do that with the Label control as when the page is posted back the content of labels are not posted to the server. You would need to make use of an input control of sorts. Probably a hidden input would be your best bet.

Comments

1

Take a hidden field like below

<asp:HiddenField ID="hdnBody" ClientIDMode="Static" runat="server" />

Then set its value in Jquery like below

<script>
function GetEmailID() {
    var bodyHtml = $("#editor").html();

    $("#hdnBody").val(bodyHtml);  
}
</script>

And in the code behind do this to get it

string body = hdnBody.Value;

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.