Asp.net Hidden field not having value in code behind, but *is* retaining value after postbacks
Posted
by
KallDrexx
on Stack Overflow
See other posts from Stack Overflow
or by KallDrexx
Published on 2012-11-29T16:28:26Z
Indexed on
2012/11/29
17:04 UTC
Read the original article
Hit count: 244
In my ASCX, I have an asp.net hidden field defined as <asp:HiddenField ID="hdnNewAsset" runat="server" />
.
In the Code Behind I have the following code:
protected void Page_Load(object sender, EventArgs e)
{
_service = new ArticleDataService(PortalId);
if (!IsPostBack)
{
string rawId = Request[ArticleQueryParams.ArticleId];
DisplayArticleDetails(rawId);
}
if (hdnNewAsset.Value.Trim() != string.Empty)
ProcessNewAsset();
}
Now, in my frontend, I have a javascript function to react to an event and set the hidden field and trigger a postback:
function assetSelected(assetGuid) {
$('input[id*="hdnNewAsset"]').val(assetGuid);
__doPostBack()
}
What's happening is that my hidden field is being set in the markup (chrome shows [
<input type=?"hidden" name=?"dnn$ctr466$Main$ctl00$hdnNewAsset" id=?"dnn_ctr466_Main_ctl00_hdnNewAsset" value=?"98d88e72-088c-40a4-9022-565a53dc33c4">?
]
for $('input[id*="hdnNewAsset"]')
).
However, when the postback occurs, hdnNewAsset.Value
is an empty string.
What's even more puzzling is that at the beginning of Page_Load
Request.Params["dnn$ctr466$Main$ctl00$hdnNewAsset"]
shows 98d88e72-088c-40a4-9022-565a53dc33c4
, and after the postback my hidden field has the same value (so the hidden field is persisting across postbacks), yet I cannot access this value via hdnNewAsset.Value
.
Can anyone see what I"m doing wrong?
© Stack Overflow or respective owner