Solved

Javascript asp.net ajax?

Posted on 2013-05-29
2
229 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 51

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

Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

ASP.Net to Oracle Connectivity Recently I had to develop an ASP.NET application connecting to an Oracle database.As I am doing it first time ,I had to solve several problems. This article will help to such developers  to develop an ASP.NET client…
IntroductionWhile developing web applications, a single page might contain many regions and each region might contain many number of controls with the capability to perform  postback. Many times you might need to perform some action on an ASP.NET po…
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…
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.

759 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

18 Experts available now in Live!

Get 1:1 Help Now