aspx页面,根据产品名称获取数据库中存储的数量
就是一文本框 输入 商品名称后 点击按钮 后面显示该商品数量
页面所有的控件都为html控件,请问用ajax如何实现

解决方案 »

  1.   

    不知道楼主说手写是什么意思。
    用jQuery就可以,而且很方便。
      

  2.   

    var xmlHttp = false;
    function myXMLHttpRequest() {    try {
            xmlHttp = new ActiveXObject("Msxml2.XMLHTTP");
        } catch (e) {
            try {
                xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
            } catch (e2) {
                xmlHttp = false;
            }
        }
        if (!xmlHttp && typeof XMLHttpRequest != 'undefined') {
            xmlHttp = new XMLHttpRequest();
        }
        return xmlHttp;
    }function pubajax(productName) {
        var url = "../Default.aspx?productName=" +productName ; //要请求的服务端地址    myXMLHttpRequest() 
        if (xmlHttp) //成功创建xmlhttprequest
        {
            xmlHttp.open("GET", url, true); //与服务端建立连接(请求方式post或get,地址,true表示异步)
            xmlHttp.onreadystatechange = callback; //指定回调函数
            xmlHttp.send(null); //发送请求
        }
    }function callback() //回调函数,对服务端的响应处理,监视response状态
    {
        if (xmlHttp.readystate == 4) //请求状态为4表示成功
        {
            if (xmlHttp.status == 200) //http状态200表示OK
            {
                Dispaly(); //所有状态成功,执行此函数,显示数据
            }
            else //http返回状态失败
            {
               // alert("服务端返回状态" + xmlHttp.statusText);
            }
        }
        else //请求状态还没有成功,页面等待
        {
            //"数据加载中";
        }
    }function Dispaly() //接受服务端返回的数据,对其进行显示
    {
        alert(xmlHttp.responseText);//xmlHttp.responseText返回的结果
    }
    在Default.aspx页面中
      protected void Page_Load(object sender, EventArgs e)
        {
            Response.CacheControl = "no-cache";
            string productName= Request.QueryString["productName"];
            //根据productName取得数量
            Response.write(数量)
     
        }
      

  3.   

    <script type="text/javascript"> 
        var xmlHttp; 
        function createXMLHttpRequest() 
        { 
            if(window.ActiveXObject) 
            { 
                xmlHttp = new ActiveXObject("Microsoft.XMLHTTP"); 
            } 
            else if(window.XMLHttpRequest) 
            { 
                xmlHttp = new XMLHttpRequest(); 
            } 
        } 
        function CheckUserName() 
        { 
          var us=document.getElementById("txtname").value; 
          if(us!="") 
          { 
            createXMLHttpRequest(); 
            var url= "RegistValidate.ashx?name="+escape(document.getElementById("txtname").value); 
            xmlHttp.open("GET",url,true); 
            xmlHttp.onreadystatechange=ShowResult; 
            xmlHttp.send(null); 
            } 
        } 
        function ShowResult() 
        { 
            if(xmlHttp.readyState==4) 
            { 
                if(xmlHttp.status==200) 
                { 
                    var s; 
                    s=xmlHttp.responseText; 
                    document.getElementByid("txT").value=s; 
                } 
            } 
        } 
    </script> public class RegistValidate : IHttpHandler
        {        public void ProcessRequest(HttpContext context)
            {
                string name = HttpUtility.UrlDecode(context.Request.QueryString["name"].ToString());
                if (查询)
                {
                    context.Response.Write(name);
                }
                else
                {
                    context.Response.Write("");
                }
               
                System.Threading.Thread.Sleep(1000);
            }        public bool IsReusable
            {
                get
                {
                    return false;
                }
            }
        }