Solved

Javascript asp.net ajax?

Posted on 2013-05-29
2
257 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 57

Accepted Solution

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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Today I had a very interesting conundrum that had to get solved quickly. Needless to say, it wasn't resolved quickly because when we needed it we were very rushed, but as soon as the conference call was over and I took a step back I saw the correct …
A long time ago (May 2011), I have written an article showing you how to create a DLL using Visual Studio 2005 to be hosted in SQL Server 2005. That was valid at that time and it is still valid if you are still using these versions. You can still re…
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…
There are cases when e.g. an IT administrator wants to have full access and view into selected mailboxes on Exchange server, directly from his own email account in Outlook or Outlook Web Access. This proves useful when for example administrator want…

688 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