简体   繁体   中英

Is it posible to get the value from an asp.net Label, that was set by javascript in the code-behind?

我使用 JavaScript 设置标签值,然后单击按钮,我想在后面的代码中访问它。

您将不得不使用隐藏字段来保存值,因为标签文本不会作为表单数据提交到服务器。

<asp:Label ID="lblName" runat="server">
<asp:Button ID="BtnClick" runat="server" onClientClick="SetName();"/>
<asp:Button ID="btnShow" runat="server" onClick="btnShow_Click"/>
<script type="text/javascript">
   function SetName()
     {
       document.getElementById("<%=lblName.ClientID%>").InnerHTML ='Hello';
     }

</script>

 From Coding:
 protected void btnShow_Click(object sender,EventArgs e)
   {
      string name = lblName.Text;
   }

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