Solved

Javascript asp.net ajax?

Posted on 2013-05-29
2
250 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 56

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 Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying 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

Suggested Solutions

For most people, the WrapPanel seems like a magic when they switch from WinForms to WPF. Most of us will think that the code that is used to write a control like that would be difficult. However, most of the work is done by the WPF engine, and the W…
The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
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…
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…

730 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