Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1604
  • Last Modified:

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.ToString();
0
Member_2_7964962
Asked:
Member_2_7964962
  • 6
  • 3
  • 2
  • +2
1 Solution
 
Russ SuterCommented:
A couple of questions:

How and when do the hidden fields receive values?
When are you checking the values in codebehind?
2
 
Member_2_7964962Author Commented:
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.
0
 
zephyr_hex (Megan)DeveloperCommented:
This is the correct format:

string UserNameValue3 = HiddenField2.Value;

Open in new window


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 :

$('#<%=HiddenField2.ClientID%>').val()

Open in new window


or

document.getElementById('<%= HiddenField2.ClientID %>');

Open in new window

0
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
Russ SuterCommented:
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.
0
 
leakim971PluritechnicianCommented:
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#
0
 
Member_2_7964962Author Commented:
Thanks for all of the suggestions.  I will apply these suggestions, and post a follow up note.  thank you.
0
 
Member_2_7964962Author Commented:
Here is the javascript function in the .aspx page


 <script type="text/javascript">
   
      window.onload = loadname();

        function loadname() {
         
            var userName = window.localStorage.getItem('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;
0
 
Julian HansenCommented:
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?
0
 
Member_2_7964962Author Commented:
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.getItem("PortalLoginApp.userName");
            alert(userName);  
            document.getElementByid('<%= hdnUserName.ClientID %>').value = userName;          
            document.getElementById("hiddenField5").innerHTML = 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;
0
 
zephyr_hex (Megan)DeveloperCommented:
This line has an lowercase "i" in getElementById, which is an error:
document.getElementByid('<%= hdnUserName.ClientID %>').value = userName;  

and this line is attempting to access innerHTML on a hidden field, which I don't believe it valid either, and it's not referencing the client id, which is a problem as well:
document.getElementById("hiddenField5").innerHTML = userName;

It should be:
document.getElementById('<%= hdnUserName.ClientID %>').value = userName;
document.getElementById('<%= hiddenField5.ClientID %>').value = userName;

For debugging, do this:

...
alert(userName);  
var hidden1 =  document.getElementById('<%= hdnUserName.ClientID %>');
console.log(hidden1);  //use console and not alert so you can inspect the contents of the object
hidden1.value = userName;
console.log(hidden1.value);
alert("Check console in F12!);

Open in new window

0
 
Julian HansenCommented:
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.
0
 
Member_2_7964962Author Commented:
I will try the above suggestions, and if the items still fail I can post the rest of my code.  Thanks.
0
 
Member_2_7964962Author Commented:
Your suggestions worked.  Thank you.
0
 
zephyr_hex (Megan)DeveloperCommented:
glad you were able to get it working.
0

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 6
  • 3
  • 2
  • +2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now