Solved

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

Posted on 2016-09-15
10
43 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 51

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
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 
LVL 51

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 51

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 51

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

6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

Join & Write a Comment

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…
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
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…

758 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

22 Experts available now in Live!

Get 1:1 Help Now