Solved

How to retrieve a client side value in the server side code behind in asp.net

Posted on 2016-09-15
10
51 Views
Last Modified: 2016-09-30
In the below code I am able to get the local storage value to the hdnUserName field, but need to get this value in code behind.  It seems as if the only way I can retrieve the hidden value from the client side is when a submit button is clicked.  Is there anyway to retrieve the hidden value in the code behind?



function pageLoad() {
       
         
         var userName = window.localStorage.getItem("PortalLoginApp.userName");
         //try the below line of code
       //  hdnUserName = userName
         //alert(userName);  
         document.getElementById('<%= hdnUserName.ClientID %>').value = userName;
         hdnUserName = document.getElementById('<%=hdnUserName.ClientID%>').value;
         //alert(hdnUserName);
         //document.getElementById("hiddenField5").innerHTML = userName;
         document.getElementById('<%= hiddenField5.ClientID %>').value = userName;
         var hidden7 = document.getElementById('<%=hdnUserName.ClientID%>').value;
         //alert(hidden7);    
               
           

        }
0
Comment
Question by:Member_2_7964962
  • 4
  • 4
10 Comments
 
LVL 30

Expert Comment

by:Alexandre Simões
ID: 41800640
Hi mate,
but when do you want the value to be sent to the server-side?

Hitting the submit button works because it will sent the page to the server and the hidden fields will be sent in the ViewState. Without triggering an action that sends the info to the server, it will never go.

Another option that you have is to do an AJAX call to the server, sending the required data.

Most of all, you need to learn about the ASP.net page lifecycle and everything should be clear :)

Cheers,
Alex
0
 

Author Comment

by:Member_2_7964962
ID: 41800661
Thanks. I do know the submit button will work, but I am wondering if there is a way to get the local storage value in the code behind. Yes, I do know the page life cycle steps, but looking for a way around this.
0
 
LVL 52

Expert Comment

by:Julian Hansen
ID: 41801050
You can get it with AJAX
<script src="http://code.jquery.com/jquery.js"></script>
<script>
$(function() {
     // This will run immediately on Page Load
     // If you want this to execute on an event then you can wrap
     // it in an event handler
     var userName = window.localStorage.getItem("PortalLoginApp.userName");
     $.ajax({
           url: /path/to/your/service',
           data: {userName: userName},
           type: 'POST' 
     }).done(function(resp) {
          // do something with the response (optional)
     });
});
</script>

Open in new window

Sample code
HTML
<input class="form-control text-right" type="text" id="username" /> 
<button class="btn btn-default" id="setLS">Set LS</button> &nbsp;
<button class="btn btn-primary" id="sendLS">Send To Server</button> &nbsp; 
<button class="btn btn-danger" id="clearLS">Clear LS</button>

Open in new window

JQuery
<script>
$(function() {
  $('#setLS').click(function(e) {
    e.preventDefault();
    var username = $('#username').val();
    window.localStorage.setItem("PortalLoginApp.userName", username);
  });
  $('#clearLS').click(function(e) {
    e.preventDefault();
    localStorage.clear();  
    $('#username').val('');
  });
  $('#sendLS').click(function(e) {
    e.preventDefault();
    var userName = window.localStorage.getItem("PortalLoginApp.userName");
    $.ajax({
           url: 't1603.php',
           data: {userName: userName},
           type: 'POST' 
    }).done(function(resp) {
          alert(resp);
    });
   });
});
</script>

Open in new window

Working sample here
0
 

Author Comment

by:Member_2_7964962
ID: 41801270
but this seems to work only when a button is clicked.  I would like the hidden value to be available in the Page Load event on the code behind.
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 52

Expert Comment

by:Julian Hansen
ID: 41801293
It seems as if the only way I can retrieve the hidden value from the client side is when a submit button is clicked.  Is there anyway to retrieve the hidden value in the code behind?
What is your actual question?

Code behind cannot access local storage - they are not visible to each other. The only way to get local storage to code behind is to send it from the client (Browser)
You do this either as
a) A Form post (submit)
b) An AJAX request

So with the above in mind - what is it you are wanting to do?
0
 

Author Comment

by:Member_2_7964962
ID: 41801338
I need to get the value of local storage when page load event in the code behind is executed. It does seem like by the comments that this isn't possible without a submit button.
0
 
LVL 52

Expert Comment

by:Julian Hansen
ID: 41801411
No you don't need a submit button you can send the value back with AJAX as soon as the page loads as I demonstrated in the first snippet of my second to last post.
This script sends the value back to the specified script when the page loads.
<script src="http://code.jquery.com/jquery.js"></script>
<script>
$(function() {
     // This will run immediately on Page Load
     var userName = window.localStorage.getItem("PortalLoginApp.userName");
     $.ajax({
           url: /path/to/your/service',
           data: {userName: userName},
           type: 'POST' 
     }).done(function(resp) {
          // do something with the response (optional)
     });
});
</script>

Open in new window

0
 

Author Comment

by:Member_2_7964962
ID: 41801479
It sounds like I would have to create a web service in order to Post the value to?
0
 
LVL 52

Accepted Solution

by:
Julian Hansen earned 500 total points
ID: 41823548
It sounds like I would have to create a web service in order to Post the value to?
Yes, something needs to be listening on the server side.
1

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

When you need to keep track of a simple list of numbers or strings, the Array object is your most direct tool.  As we saw in my earlier EE Article (http://www.experts-exchange.com/A_3488.html), typical array handling might look like this: (CODE) B…
This article discusses the difference between strict equality operator and equality operator in JavaScript. The Need: Because JavaScript performs an implicit type conversion when performing comparisons, we have to take this into account when wri…
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…

920 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

15 Experts available now in Live!

Get 1:1 Help Now