I'm having some issues setting a value to a HiddenField in ASP.NET 4.5.
From what I've seen I've tried the following without any luck:
In ASPX:
<asp:HiddenField ID="HiddenField" runat="server" value="" />
<script type="text/javascript">
function SetHiddenField() {
var vv = "HELLO WORLD";
document.getElementById('<%=HiddenField.ClientID%>').value = vv;
}
</script>
In code-behind:
ScriptManager.RegisterStartupScript(this.Page, this.GetType(), "SetHiddenField", "SetHiddenField();", true);
ScriptManager.RegisterStartupScript(this.Page, this.GetType(), "alert", "alert('" + HiddenField.ClientID + "');", true);
This alerts garbage in the ClientID.
The other solution I've tried is the following.
In .ASPX:
<asp:HiddenField ID="HiddenField" runat="server" value="" />
<script type="text/javascript">
function SetHiddenField() {
var vv = "HELLO WORLD";
document.getElementById('HiddenField').value = vv;
}
</script>
One issue here is that .value
does not exist in the IntelliSense, only .ValueOf
.
In code-behind:
ScriptManager.RegisterStartupScript(this.Page, this.GetType(), "SetHiddenField", "SetHiddenField();", true);
ScriptManager.RegisterStartupScript(this.Page, this.GetType(), "alert", "alert('" + HiddenField.Value + "');", true);
Nothing happens, probably an error in the JavaScript, since no alert is shown.
Can anyone point me to the right direction, please?
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…