简体   繁体   English

在javascript中设置innerHTML并从C#获取

[英]Set innerHTML in javascript and get from C#

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: 我通过javascript设置innerHTML:

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
}

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: 通过执行以下操作在隐藏字段中设置innerHtml值:

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

You can then access it from server side by doing: 然后,您可以通过以下操作从服务器端访问它:

TextBox1.Text = Hidden1.Value;

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. 您无法使用Label控件执行此操作,因为当页面回发时,标签的内容不会发布到服务器。 You would need to make use of an input control of sorts. 您需要使用各种输入控件。 Probably a hidden input would be your best bet. 隐藏输入可能是您最好的选择。

Take a hidden field like below 采取如下隐藏的字段

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

Then set its value in Jquery like below 然后在Jquery中设置它的值,如下所示

<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;

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

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