Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Javascript asp.net ajax?

Posted on 2013-05-29
2
Medium Priority
?
269 Views
Last Modified: 2014-01-07
I have a situation where I need to use the text entered into a textbox from a javascript function.  When the page refreshes I lose the text that has been entered into label.  I'm using a label becaue this will be a ipad application and I'm trying to keep the keyboard from popping up automatically.  I have never used ajax from a javascript function before, however I believe that's what I'll need in order to post the text to the server before the page refreshes.  I'm capturing the key codes and then having the text displayed in a label or readonly textbox.
Can someone tell me what I need to do in order to post the text from the label before the page refreshes?  I'm posting my code so you can see what I'm trying to accomplish.

Thanks

<script type="text/javascript">
        function displayunicode(e) {
            var unicode = e.keyCode ? e.keyCode : e.charCode
            var lb2 = document.getElementById("Label2").innerHTML
                        
            switch (unicode) {
                case 96: document.getElementById("Label2").innerHTML = lb2 + "0"; break;
                case 97: document.getElementById("Label2").innerHTML = lb2 + "1"; break;
                case 98: document.getElementById("Label2").innerHTML = lb2 + "2"; break;
                case 99: document.getElementById("Label2").innerHTML = lb2 + "3"; break;
                case 100: document.getElementById("Label2").innerHTML = lb2 + "4"; break;
                case 101: document.getElementById("Label2").innerHTML = lb2 + "5"; break;
                case 102: document.getElementById("Label2").innerHTML = lb2 + "6"; break;
                case 103: document.getElementById("Label2").innerHTML = lb2 + "7"; break;
                case 104: document.getElementById("Label2").innerHTML = lb2 + "8"; break;
                case 105: document.getElementById("Label2").innerHTML = lb2 + "9"; break;
                case 110: document.getElementById("Label2").innerHTML = lb2 + "."; break;

                default: ;

            }

            var url = "default.aspx?txtaccum=" + document.getElementById("Label2").innerHTML;

            var xmlHttp = new XMLHttpRequest();

            xmlHttp.open("GET", url, true);

            xmlHttp.onreadystatechange = function () {
                if (xmlHttp.readyState == 4) {
                    
                    if (xmlHttp.status == 200) {
                        alert(xmlHttp.responseText);
                    }
                } 
            };
            
            xmlHttp.send();
        }
    </script>

<body onkeyup="displayunicode(event); this.select()">

Open in new window

0
Comment
Question by:brettawv
2 Comments
 
LVL 60

Accepted Solution

by:
Julian Hansen earned 2000 total points
ID: 39206929
Do yourself a favour and use JQuery

This demonstrates the concept

<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js"></script>
<script type="text/javascript">
function displayunicode(e) {
  var unicode = e.keyCode ? e.keyCode : e.charCode
  var lb2 = document.getElementById("Label2").innerHTML

  switch (unicode) {
    case 96: document.getElementById("Label2").innerHTML = lb2 + "0"; break;
    case 97: document.getElementById("Label2").innerHTML = lb2 + "1"; break;
    case 98: document.getElementById("Label2").innerHTML = lb2 + "2"; break;
    case 99: document.getElementById("Label2").innerHTML = lb2 + "3"; break;
    case 100: document.getElementById("Label2").innerHTML = lb2 + "4"; break;
    case 101: document.getElementById("Label2").innerHTML = lb2 + "5"; break;
    case 102: document.getElementById("Label2").innerHTML = lb2 + "6"; break;
    case 103: document.getElementById("Label2").innerHTML = lb2 + "7"; break;
    case 104: document.getElementById("Label2").innerHTML = lb2 + "8"; break;
    case 105: document.getElementById("Label2").innerHTML = lb2 + "9"; break;
    case 110: document.getElementById("Label2").innerHTML = lb2 + "."; break;

    default: ;

  }

  // Or use $.get if you are expecting GET vars on the server side
  $.post('default.aspx',{txtaccum: $('#Label2').html()}, function(response) {
    // do something with the response from server if you need to or just ignore if there is nothing coming back
  });
}
</script>

Open in new window

0
 
LVL 75

Expert Comment

by:Michel Plungjan
ID: 39206996
If jquery is to be used, use it all the way!

$(function() {
  $(document).on("keyup", function(e) {
     var key = e.which;
     if (key && key >= 96 && key <= 110)) $("#Label2").append(String.fromCharCode(key));
     }
   });
   ...
});
0

Featured Post

NEW Veeam Agent for Microsoft Windows

Backup and recover physical and cloud-based servers and workstations, as well as endpoint devices that belong to remote users. Avoid downtime and data loss quickly and easily for Windows-based physical or public cloud-based workloads!

Question has a verified solution.

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

Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
Hello there! As a developer I have modified and refactored the unit tests which was written by fellow developers in the past. On the course, I have gone through various misconceptions and technical challenges when it comes to implementation. I would…
This is Part 3 in a 3-part series on Experts Exchange to discuss error handling in VBA code written for Excel. Part 1 of this series discussed basic error handling code using VBA. http://www.experts-exchange.com/videos/1478/Excel-Error-Handlin…
Despite its rising prevalence in the business world, "the cloud" is still misunderstood. Some companies still believe common misconceptions about lack of security in cloud solutions and many misuses of cloud storage options still occur every day. …

824 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