?
Solved

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

Posted on 2016-09-15
10
Medium Priority
?
134 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 58

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
Get real performance insights from real users

Key features:
- Total Pages Views and Load times
- Top Pages Viewed and Load Times
- Real Time Site Page Build Performance
- Users’ Browser and Platform Performance
- Geographic User Breakdown
- And more

 

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 58

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 58

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 58

Accepted Solution

by:
Julian Hansen earned 2000 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

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

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

Article by: DanRollins
This article describes a JavaScript program that creates a maze made of hexagonal cells.  In Part 2 (http://www.experts-exchange.com/Programming/Languages/Scripting/JavaScript/A_7850-Hex-Maze-Part-2.html), we'll extend the program by adding a depth-…
Originally, this post was published on Monitis Blog, you can check it here . In business circles, we sometimes hear that today is the “age of the customer.” And so it is. Thanks to the enormous advances over the past few years in consumer techno…
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…
Suggested Courses

752 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