HiddenField
is an ASP.NET
Server Control
, so you can't access them by directly referencing their ID. To access them in
JavaScript
, you would need to use the function
document.getElementById and pass along the ID of the control.
For example:
document.getElementById('<%=hidAnswer.ClientID%>').value = 'set whatever value here';
Note that we are using the
ClientID
, instead of the
ID
of the
HiddenField
, the reason for this is because ASP.NET will render a different ID in the markup especially if your control is nested within a
NamingContainer
. So the actual ID could look something like
clt0_clt1_hidAnwser
.
In .NET 4.x, The
ClientMode
property was introduced to avoid the autogenerated ids being rendered See:
Control.ClientIDMode Property (System.Web.UI) | Microsoft Docs[
^]
So if you set
ClientMode= "Static"
for a Server Control, then you can safely use something like this to access the control:
document.getElementById('hidAnswer').value = 'set whatever value here';