Ajax 实现局部刷新、无刷新

page1.aspx前台代码:

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="page1.aspx.cs" Inherits="page1" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
    <title>无标题页</title>
    <script type="text/javascript">
      var xmlHttp;
      var xml_post_get;
     
      function CreateXmlHttp()
      {
        if(window.ActiveXObject)
        {
          xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
          xml_post_get="post"; 
        }else if(window.XMLHttpRequest)
        {
          xmlHttp=new XMLHttpRequest();
          xml_post_get="get";
        }
      }
     
    function getData()
    {
      CreateXmlHttp();
      //获取用户填写的名称
      var city=document.getElementById("s1").value;
     
      //加载要链接的页面
      xmlHttp.Open(xml_post_get,"page1.aspx?city="+escape(city),true);
     
            //将对象状态与事件相关联
      xmlHttp.onreadystatechange=stateChange;
     
      //发送请求
      xmlHttp.Send(null);
    }
   
    function stateChange()
    {
      if(xmlHttp.readystate==4)
      {
        if(xmlHttp.status==200)
        {
          FillData(xmlHttp.responseText);
        }
      }
    }
   
    function FillData(strcity)
    {
       document.getElementById("s2").options.length=0;
       var indexofcity;
       var city;
       //切割传递来的字符串
       while(strcity.length>0)
       {
       //判断是否是最后一个字符串
        indexofcity=strcity.indexOf(",");
        if(indexofcity >0)
        {
        city=strcity.substring(0,indexofcity);
        strcity=strcity.substring(indexofcity+1);
        //填充下拉框
        document.getElementById("s2").add(new Option(city,city));
        }
        else
        {
        // 如果是最后一个字符串
           lastcity=strcity.substring(0,2);
           document.getElementById("s2").add(new Option(lastcity,lastcity));
           break;
        }
       };
    }
     
     
    
    </script>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        省/市:<select id="s1" style="width: 93px" οnchange="getData()">
            <option>--请选择--</option>
            <option value="北京">北京</option>
            <option value="上海">上海</option>
            <option value="济南">济南</option>
            <option value="其他">其他</option>
        </select>&nbsp;
        地区:<select id="s2" style="width: 90px">
            <option></option>
        </select>
        <input id="Button1" type="button" value="测试" οnclick="getData()" />
    </div>
    </form>
</body>
</html>

page1.aspx.cs后台代码:

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 page1 : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {

        if (Request.QueryString["city"] != null)
        {
            //获取传递过来的参数
            string city = Request.QueryString["city"];
            //判断城市名
            switch (city)
            {
                case "北京":
                    //填充相关的区域
                    Response.Write("朝阳,海淀,东城,西城");
                    break;
                case "上海":
                    Response.Write("浦东,静安,虹口,徐汇");
                    break;
                case "济南":
                    Response.Write("历下,历城,市中,天桥");
                    break;
                default:
                    Response.Write("其他");
                    break;
            }
           
Response.End();
        }

    }
}

 

测试效果:

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值