Solved

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

Posted on 2016-09-15
10
83 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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 56

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
Instantly Create Instructional Tutorials

Contextual Guidance at the moment of need helps your employees adopt to new software or processes instantly. Boost knowledge retention and employee engagement step-by-step with one easy solution.

 

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
 
LVL 56

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 56

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 56

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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Triple line in an alert message 4 24
replacing inline javascript with jquery 4 46
Session on Html 8 42
Check input text, Number 7 35
In this article, we'll look how to sort an Array in JavaScript, including the more advanced techniques of sorting a collection of records either ascending or descending on two or more fields. Basic Sorting of Arrays First, let's look at the …
I've been trying to accomplish this for a while and it just struck me yesterday how to accomplish this task. I have done searches all over the internet looking for ways to email pages from my applications and finally I have done it!!! Every single s…
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…

726 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