动态显示文本框的字符串数(ajax入门小程序) 


首先添加一个html页其页面代码如下:
<html>
<head>
<title>在ASP.NET的第一个Ajax小程序</title>
<link rel="stylesheet" type="text/css" href="../css/main.css"/>
<script language="javascript">
        
var xmlHttp
        
function showHint(str)
        
{
            
if (str.length > 0)
            
{
                
var url="GetRequest.aspx?name=" + str
                
if (window.ActiveXObject) 
                    xmlHttp 
= new ActiveXObject("Microsoft.XMLHTTP"); 
                }

                  xmlHttp.onreadystatechange
=stateChanged
                xmlHttp.open(
"POST", url , true)
                xmlHttp.send(
null)
            }

            
else
            
{
                document.getElementById(
"txtHint").innerHTML=""
            }

        }

        
function stateChanged()
        
{
            
if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete")
            
{
                document.getElementById(
"txtHint").innerHTML=xmlHttp.responseText
            }

        }


</script>
</head><body>
<div class=content>
<h1>纯Ajax实现的小程序</h1>
<form>
    
<p>输入一个字符串:<input type="text" id="txt1"onkeyup="showHint(this.value)"></p>

<p>
    字符串的长度: 
<span id="txtHint"></span></p> 
   
</form> </div></body>
</html>


述代码中GetRequest.aspx?name=" + str 指明将请求页面GetRequest.aspx并把用户输入的字符串传递给页面GetRequest.aspx,所以下一步就是建立GetRequest.aspx页面计算字符串长度,后台代码如下:
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
public partial class PureAjax_GetRequest : System.Web.UI.Page
{
    
protected void Page_Load(object sender, EventArgs e)
    
{
        
if (Request.QueryString["name"!= "")
            Response.Write(Request.QueryString[
"name"].Length);
        
else
            Response.Write(
"NULL");
    }

}

posted @ 2007-09-11 08:45    阅读(343)  评论(0编辑  收藏  举报