Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Javascript asp.net ajax?

Posted on 2013-05-29
2
Medium Priority
?
265 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
[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
2 Comments
 
LVL 59

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

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

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

Just a quick little trick I learned recently.  Now that I'm using jQuery with abandon in my asp.net applications, I have grown tired of the following syntax:      (CODE) I suppose it just offends my sense of decency to put inline VBScript on a…
This article shows how to deploy dynamic backgrounds to computers depending on the aspect ratio of display
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…
Do you want to know how to make a graph with Microsoft Access? First, create a query with the data for the chart. Then make a blank form and add a chart control. This video also shows how to change what data is displayed on the graph as well as form…

662 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