Solved

How to access the value of a hidden field in code behind using c#

Posted on 2016-09-13
14
66 Views
Last Modified: 2016-09-15
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
Comment
Question by:Member_2_7964962
  • 6
  • 3
  • 2
  • +2
14 Comments
 
LVL 20

Expert Comment

by:Russ Suter
ID: 41796808
A couple of questions:

How and when do the hidden fields receive values?
When are you checking the values in codebehind?
2
 

Author Comment

by:Member_2_7964962
ID: 41796923
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
 
LVL 42

Expert Comment

by:zephyr_hex
ID: 41796947
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
 
LVL 20

Expert Comment

by:Russ Suter
ID: 41797036
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
 
LVL 82

Expert Comment

by:leakim971
ID: 41797160
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
 

Author Comment

by:Member_2_7964962
ID: 41797205
Thanks for all of the suggestions.  I will apply these suggestions, and post a follow up note.  thank you.
0
 

Author Comment

by:Member_2_7964962
ID: 41798329
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
Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

 
LVL 51

Expert Comment

by:Julian Hansen
ID: 41798405
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
 

Author Comment

by:Member_2_7964962
ID: 41798686
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
 
LVL 42

Accepted Solution

by:
zephyr_hex earned 500 total points
ID: 41798882
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
 
LVL 51

Expert Comment

by:Julian Hansen
ID: 41798890
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
 

Author Comment

by:Member_2_7964962
ID: 41798926
I will try the above suggestions, and if the items still fail I can post the rest of my code.  Thanks.
0
 

Author Closing Comment

by:Member_2_7964962
ID: 41799690
Your suggestions worked.  Thank you.
0
 
LVL 42

Expert Comment

by:zephyr_hex
ID: 41800103
glad you were able to get it working.
0

Featured Post

What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

Join & Write a Comment

Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!
This article discusses how to create an extensible mechanism for linked drop downs.
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

708 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now