Asp.net无刷新二级联动2

Asp.net无刷新二级联动2

 

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

<!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>
</head>
<script language="javascript">

function load(provinceid)
{
var drp2 = document.getElementById("DropDownList2");
var len=drp2.options.length;
while(len>0)
{
    drp2.options.remove(0);
    len=drp2.options.length;
}
   
var oHttpReq = new ActiveXObject("MSXML2.XMLHTTP");    //发送请求
var oDoc = new ActiveXObject("MSXML2.DOMDocument");    //响应结果
   
if(provinceid==null)
{
    var provinceid=document.getElementById("DropDownList1").value;
}
   
oHttpReq.open("post","Default9.aspx?pid="+provinceid, false);
oHttpReq.send("");
       
result = oHttpReq.responseText;
oDoc.loadXML(result);
   
items = oDoc.selectNodes("//city/Table");
for (var item = items.nextNode();item;item = items.nextNode())
{ 
    var city = item.selectSingleNode("cityname").nodeTypedValue;
    var id = item.selectSingleNode("id").nodeTypedValue;
    var newOption = document.createElement("OPTION");
    newOption.text = city;
    newOption.value = id;
    drp2.options.add(newOption);
} 
}
  
</script>
<body>
    <form id="form1" runat="server">
    <div>
    <asp:DropDownList id="DropDownList1" style="Z-INDEX: 101; LEFT: 88px; POSITION: absolute; TOP: 32px" runat="server"></asp:DropDownList>
<asp:DropDownList id="DropDownList2" style="Z-INDEX: 102; LEFT: 176px; POSITION: absolute; TOP: 32px" runat="server"></asp:DropDownList>

    </div>
    </form>
</body>
</html>

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;
using System.Data.SqlClient;
using System.Xml;

public partial class Default8 : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!this.IsPostBack)
        {
            SqlConnection con = new SqlConnection(ConfigurationSettings.AppSettings["gipsy"]);
            SqlDataAdapter da = new SqlDataAdapter("select * from province", con);
            DataSet ds = new DataSet("province");
            da.Fill(ds);

            this.DropDownList1.DataSource = ds;
            this.DropDownList1.DataTextField = "provinceName";
            this.DropDownList1.DataValueField = "id";
            this.DropDownList1.DataBind();

            this.DropDownList1.Attributes.Add("onchange", "load(this.options[this.selectedIndex].value)");
        }
    }

}


另一个页面的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;
using System.Xml;
using System.Data.SqlClient;

public partial class Default9 : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (Request["pid"] != null)
        {
            int intpid = Convert.ToInt32(Request["pid"]);
            SqlConnection con = new SqlConnection(ConfigurationSettings.AppSettings["gipsy"]);
            SqlDataAdapter da = new SqlDataAdapter("select id,cityname from city where provinceid=" + intpid + "", con);
            DataSet ds = new DataSet("city");
            da.Fill(ds);
            XmlTextWriter writer = new XmlTextWriter(Response.OutputStream, Response.ContentEncoding);
            writer.Formatting = Formatting.Indented;
            writer.Indentation = 4;
            writer.IndentChar = ' ';
            ds.WriteXml(writer);
            writer.Flush();
            Response.End();
            writer.Close();
        }
    }
}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值