Pages

Wednesday, April 18, 2012

Simple AJAX Script


<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title>Untitled Page</title>
    <script type="text/javascript">
    function ShowSuggestion(str) {
        debugger;
       
        var xmlHttp = null;
        if (str.toString().length == 0) {
            document.getElementById("spanSuggestion").innerHTML = "No results";
            return;
        }
        if (window.XMLHttpRequest) {
            xmlHttp = new XMLHttpRequest();
           
            xmlHttp.onreadystatechange = function() {
           
            if (xmlHttp.readyState == 4) {
                document.getElementById("spanSuggestion").innerHTML = xmlHttp.responseText;
                }
            }
           
            var url = "Search.aspx?q=" + str;
            xmlHttp.open("GET", url, false);
            xmlHttp.send(null);
           
            document.getElementById("spanSuggestion").innerHTML = xmlHttp.responseText;
        }       
    }
    </script>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <h2>
            Welcome Ajax baby!</h2>
        <input type="text" id="txt1" onkeyup="ShowSuggestion(this.value)" />
        <p>
            Suggestions:</p>
        <span id="spanSuggestion"></span>
    </div>
    </form>
</body>
</html>


protected void Page_Load(object sender, EventArgs e)
        {
            LtoSDataContext dataContext = new LtoSDataContext();
            var desc = dataContext.SP_GetStaticInformationDescription();
            string q = Request.QueryString["q"];
            string result = (from d in desc where d.ShortDescription.StartsWith(q) select d.ShortDescription).FirstOrDefault();
            if (result == null)
                Response.Write("No suggestions available");
            else Response.Write(result);
        }



No comments:

Post a Comment