1

Im trying to request javascript variable and use it in code behind, but it always has the value of "" Im not sure whats wrong, it should work based on examples I have looked at. Any help - or different methods to request Js variable to .net webforms (c#) would be very much appreciated, thanks.

<script type="text/javascript">
function SetPostbackValues() {
    var myvalue = "abcdefg";
    document.getElementById("hiddenControl1").value = myvalue;
}
</script>

<div class="jumbotron">
    <input id="hiddenControl1" type="hidden" runat="server" />
    <asp:Button ID="Button1" runat="server"  OnClientClick="SetPostbackValues()" OnClick="Button1_Click" Text="Button" />
</div>

 ....
 protected void Button1_Click(object sender, EventArgs e)
 {
    var hiddenValue = hiddenControl1.Value;
    //use value
 }
mpalencia
  • 5,481
  • 4
  • 45
  • 59
user2164182
  • 63
  • 10

1 Answers1

0

Its working now I changed the following:

<input id="hiddenControl1" type="hidden" runat="server" ClientIDMode="Static"/>

document.getElementById("<%= hiddenControl1.ClientID %>").value = myvalue;
user2164182
  • 63
  • 10