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
Solved

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

Posted on 2016-09-15
10
74 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 55

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
Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 

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 55

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 55

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 55

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

Active Directory Webinar

We all know we need to protect and secure our privileges, but where to start? Join Experts Exchange and ManageEngine on Tuesday, April 11, 2017 10:00 AM PDT to learn how to track and secure privileged users in Active Directory.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
how can i get the string decoded ? 2 28
html Uncheck Checkbox 2 25
What if i make webbased alternative for MS Access 5 71
jQuery force form POST 7 48
Avoid defining the variables in the global scope; trying to define them in a local function scope. Because:   • Look-up is performed every time a variable is accessed.   • Variables are resolved backwards from most specific to least specific scope…
Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
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…

828 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