Sources Server

Back to Home

Directory List for the Web Site:





File List for the Selected Directory:

 






<%@ Page Language="C#" 
    MasterPageFile="~/MasterPage.master" 
    AutoEventWireup="true" 
    CodeFile="AjaxString.aspx.cs" 
    Inherits="_Default" 
    Title="Adams String Concat Page" %>
        
<asp:Content ID="Content1" ContentPlaceHolderID="Body" Runat="Server">
    <script type="text/javascript" language='Javascript'>
    // Some code adapted from : http://www.jibbering.com/2002/4/httprequest.html
    var xmlHttp=false;
    /*@cc_on @*/
    /*@if (@_jscript_version >= 5)
    // JScript gives us Conditional compilation, we can cope with old IE versions.
    // and security blocked creation of the objects.
     try {
      xmlHttp = new ActiveXObject("Msxml2.XMLHTTP");
     } catch (e) {
      try {
       xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
      } catch (E) {
       xmlHttp = false;
      }
     }
    @end @*/
    
    function rConcat() {
    // Create the request object try standard, then microsoft, else fail
    if (!xmlHttp && typeof XMLHttpRequest!='undefined') {
        try {
            xmlHttp = new XMLHttpRequest();
        } catch (e) {
            xmlHttp = false;
        }
    }

    // Try again with old-school property
    if(!xmlHttp && window.createRequest) {
        try {
            xmlHttp = window.createRequest();
        } catch (e) {
            xmlHttp = false;
        }
    } 
   
    if (xmlHttp == false) {
        alert("Your browser does not support AJAX.")
        return;
    }
    
    // Make the query string
    var eA = document.getElementById("<%=TextBox1.ClientID%>").value;
    var eB = document.getElementById("<%=TextBox2.ClientID%>").value;
    var query = "Callback.aspx?A=" + eA + "&B=" + eB;
    
    // Open connection
    xmlHttp.open("GET", query , true);
    // Request is ready with value "4"
    xmlHttp.onreadystatechange = function () {
        if (xmlHttp.readyState == 4) {
            // Set RTT RPC time
            var stime = document.getElementById("<%=HiddenTime.ClientID%>").value;
            var date = new Date();
            document.getElementById("<%=TextBox4.ClientID%>").value = date.getTime() - parseInt(stime);
            
            
            //document.forms[0].elements["<%=TextBox3.ClientID%>"].value=xmlHttp.responseText;
            document.getElementById("<%=TextBox3.ClientID%>").value=xmlHttp.responseText;
            
        }
    }

    var date = new Date();
    document.getElementById("<%=HiddenTime.ClientID%>").value = date.getTime();
    
    xmlHttp.send(null);
}
    </script>
    <h2>AJAX String Concatenator</h2><br />
    This is an overly complicated proof of concept.  It accomplishes, using heavy
    JavaScript, two ASP pages and a callback, what could have been done with either
     a few lines of simple client-only JavaScript or a single form post back.  The
     point, however, was not do make a string concatenator, but to utilize/learn AJAX<p/>
     
    As noted above, this is a proof of concept that functions similiar to the
    "suggest as I type" feature seen in Google Suggest Beta and other products.<p />
    <b>UPDATE:</b> This demo apparently doesnt like some IE versions (i think 7).  
    This demo was designed for Firefox and Safari because IE uses some strange AJAX methods.
    The AJAX here is NOT from the Microsoft ASP toolkit because it was written before that was
    added to this server. It was hand written by me in a mix of JavaScript and ASP so that I 
    could learn how this stuff works.<p/>
    <form action="Default.aspx" method="post">
        <asp:TextBox ID="TextBox1" runat="server" 
                    OnKeyUp="javascript:rConcat();"></asp:TextBox>
        &lt;-- type somthing here
        <p/>
        <asp:TextBox ID="TextBox2" runat="server"
                    OnKeyUp="javascript:rConcat();"></asp:TextBox>
        &lt;-- and here
        <p/>
        <asp:TextBox ID="TextBox3" runat="server"
                    ReadOnly="true" Text="Result Will Be Here"></asp:TextBox>
        &lt;-- see them combined here
        <p/>
        Server Response Time (ms):
        <asp:TextBox ID="TextBox4" runat="server"
                style="background-color: inherit; width: 70px; border: none;"
                ReadOnly="true" Text="(time)"></asp:TextBox>
        <input type="hidden" ID="HiddenTime" runat="server"></asp:TextBox>
    </form>
</asp:Content>