Member_2_7964962_1
asked on
How to access the value of a hidden field in code behind using c#
The following two hidden fields are in the asp.net side of the code,
<asp:HiddenField ID="HiddenField2" runat="server" />
<asp:HiddenField ID="HiddenField1" runat="server" />
I need to be able to access the value of the Hidden field and assign to a variable, in asp.net code behind using c#. The values are coming back null.
string UserNameValue3 = HiddenField2.Value;
string UserNameValue4 = HiddenField1.Value.ToStrin g();
<asp:HiddenField ID="HiddenField2" runat="server" />
<asp:HiddenField ID="HiddenField1" runat="server" />
I need to be able to access the value of the Hidden field and assign to a variable, in asp.net code behind using c#. The values are coming back null.
string UserNameValue3 = HiddenField2.Value;
string UserNameValue4 = HiddenField1.Value.ToStrin
ASKER
I am passing values to the HiddenFields by retrieving local storage value using javacript. I am checking the values on page load event in code behind.
If you need additional code I can post this code tomorrow when I have access to the computer code.
If you need additional code I can post this code tomorrow when I have access to the computer code.
This is the correct format:
If that is not working, then please verify you're not attempting to access the hidden field's value prior to the Page_Load event.
Also, please verify that you're setting the hidden field's value from JavaScript using the correct syntax. ASP controls have bizarre ID's, so you can't just do getElementById("controlID" ) or $("#controlID").val(). You need to reference the ClientID of the control, such as :
or
string UserNameValue3 = HiddenField2.Value;
If that is not working, then please verify you're not attempting to access the hidden field's value prior to the Page_Load event.
Also, please verify that you're setting the hidden field's value from JavaScript using the correct syntax. ASP controls have bizarre ID's, so you can't just do getElementById("controlID"
$('#<%=HiddenField2.ClientID%>').val()
or
document.getElementById('<%= HiddenField2.ClientID %>');
So what I suspect is happening here is that you have some kind of button click event and you're trying to read the value from there. Because of how the aspx page lifecycle works this will yield a null. What you need to do is put the logic in the Page_Load method and check the IsPostback property to determine whether or not you need to check the value.
1 - user call your page
2 - server run C# code retrieving HiddenField1 & 2 values
3 - server send page to browser
4 - browser execute javascript to read localstorage and set HiddenField1 & 2
happy(not really) end.
so as you can see you read your hidden before javascript set it as mentionned by zephyr_hex
maybe use Cookie instead localstorage as you can access browser Cookies with c#
2 - server run C# code retrieving HiddenField1 & 2 values
3 - server send page to browser
4 - browser execute javascript to read localstorage and set HiddenField1 & 2
happy(not really) end.
so as you can see you read your hidden before javascript set it as mentionned by zephyr_hex
maybe use Cookie instead localstorage as you can access browser Cookies with c#
ASKER
Thanks for all of the suggestions. I will apply these suggestions, and post a follow up note. thank you.
ASKER
Here is the javascript function in the .aspx page
<script type="text/javascript">
window.onload = loadname();
function loadname() {
var userName = window.localStorage.getIte m('Value') ;
document.getElementByid('< %= hdnUserName.ClientID %>').value = userName;
var HiddenField1 = localStorage.getItem(Key);
document.getElementByid("< %= HiddenField1.ClientID %>").value = HiddenField1;
}
</script>
and here is the code in the code behind page to get the value of the Hidden field
I tested this value in the Page load event as well as the Submit button event, but the value is Null for both values below.
string UserNameValue3 = hdnUserName.Value;
string UserNameValue4 = HiddenField1.Value;
<script type="text/javascript">
window.onload = loadname();
function loadname() {
var userName = window.localStorage.getIte
document.getElementByid('<
var HiddenField1 = localStorage.getItem(Key);
document.getElementByid("<
}
</script>
and here is the code in the code behind page to get the value of the Hidden field
I tested this value in the Page load event as well as the Submit button event, but the value is Null for both values below.
string UserNameValue3 = hdnUserName.Value;
string UserNameValue4 = HiddenField1.Value;
Are you able to check the hidden values before you submit the form by using the Console (F12)
Are you sure the hidden variables are inside the form?
Are you sure the hidden variables are inside the form?
ASKER
I was able to read the local storage value in the following JavaScript code in the .aspx page,
<input type="hidden" value="" id="hdnUserName" runat="server" />
<input type="hidden" value="" id="hiddenField5" runat="server" />
<script type="text/javascript">
function pageLoad() {
var userName = window.localStorage.getIte m("PortalL oginApp.us erName");
alert(userName);
document.getElementByid('< %= hdnUserName.ClientID %>').value = userName;
document.getElementById("h iddenField 5").innerH TML = userName;
}
</script>
but having difficulty passing the hidden field to the code behind. Here is a snipped of the code behind.
string UserNameValue3 = hdnUserName.Value;
string UserNameValue5 = hiddenField5.Value;
<input type="hidden" value="" id="hdnUserName" runat="server" />
<input type="hidden" value="" id="hiddenField5" runat="server" />
<script type="text/javascript">
function pageLoad() {
var userName = window.localStorage.getIte
alert(userName);
document.getElementByid('<
document.getElementById("h
}
</script>
but having difficulty passing the hidden field to the code behind. Here is a snipped of the code behind.
string UserNameValue3 = hdnUserName.Value;
string UserNameValue5 = hiddenField5.Value;
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
but having difficulty passing the hidden field to the code behind. Here is a snipped of the code behind.Did you try the suggestion above - check the Console before submit to see if the hidden values are getting populated.
Two possibilities
1. The values are not being populated
2. They are but not being included in the post back.
To check in the console.
1. run code to extract values from localstorage and set hidden values
2. before submit F12 find hidden input controls in HTML tab and check their values.
Failing that can we see the rest of your code for the page.
ASKER
I will try the above suggestions, and if the items still fail I can post the rest of my code. Thanks.
ASKER
Your suggestions worked. Thank you.
glad you were able to get it working.
How and when do the hidden fields receive values?
When are you checking the values in codebehind?