Have you put the <script>
tag on the head of the page?
Because if you put and are trying to assign a value will not work, the following error occurs:
Uncaught TypeError: Can not set property 'value' of null
Because you have not loaded the page completely, you will not be able to assign any value to a page.
To solve this you can by <script>
at the end of <body>
, example:
Page.aspx :
<asp:Content ID="Content1" ContentPlaceHolderID="HeadContent" runat="server">
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="server">
<asp:HiddenField ID="HiddenField1" runat="server" />
<asp:Button ID="Button1" runat="server" Text="Button" OnClick="Button1_Click" />
<script>
document.getElementById("<%= HiddenField1.ClientID.ToString() %>").value = 'Token';
</script>
</asp:Content>
Code Behind :
protected void Button1_Click(object sender, EventArgs e)
{
Response.Write(HiddenField1.Value.ToString());
}