使用XMLHTTP实现DropDownList的不刷新显示

今天研究Ajax实现不刷新联动显示数据,没搞明白,但是使用XMLHTTP实现DropDownList的不刷新成功了!郁闷的心情好了一点!

这个方法有挺大的缺点,就是牺牲页面,参数直接暴露在他人眼下,暂时也没别的办法,先看看再说!

现在把代码放上,供大家参考:

注:ddl是DropDownList的简写!

ddl所在页面脚本:

<script language=javascript>
      function load(state)
      {
        var dr2=document.getElementById("dlshi");
//        alert(dr2.options.length);
//        for(var i=0;i<=dr2.options.length;i++)
//        {
//          dr2.options.remove(i);
//        }
          dr2.length=0;
        var oHttpReq=new ActiveXObject("MSXML2.XMLHTTP");
        var odoc=new ActiveXObject("MSXML2.DOMDocument");
     
        var state=document.getElementById("dlsheng").value;
       
        oHttpReq.open("POST","Frm_Detail.aspx?state="+state,false);
       
        oHttpReq.send("");
      
        result=oHttpReq.responseText;
      // alert(result);
        odoc.loadXML(result);
//        alert(result);
        items=odoc.selectNodes("//Table");
        for(var j=0;j<=items.length;j++)
        {
           var city=items[j].text;
          // alert(city);
           var newoption=document.createElement("option");
           newoption.text=city;
           newoption.value=city;
           dr2.options.add(newoption);
        }
      }
    </script>

在主动ddl添加 οnchange="load();

CS代码:

protected void Page_Load(object sender, EventArgs e)
    {
     if (!IsPostBack)
        {

            SqlConnection conzxpx;
            SqlCommand cmdSelect;
            SqlDataReader dtrt_dic;

            conzxpx = new SqlConnection(ConfigurationSettings.AppSettings["ConnectionString"].ToString());
            cmdSelect = new SqlCommand("select c_code,c_name from t_dic where c_code like '%0000'", conzxpx);
            conzxpx.Open();
            dtrt_dic = cmdSelect.ExecuteReader();
            dlsheng.DataSource = dtrt_dic;
            dlsheng.DataTextField = "c_name";
            dlsheng.DataValueField = "c_code";
            dlsheng.DataBind();
            dtrt_dic.Close();

-------------------------------

在"存储"数据的界面(牺牲界面,我们暂时这样称呼它)放置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.Data.SqlClient;
using System.Xml;
public partial class Frm_Detail : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!this.IsPostBack)
        {
            string state = this.Request.QueryString["state"].Trim();
            System.Data.SqlClient.SqlDataAdapter adapter = new SqlDataAdapter();
            System.Data.SqlClient.SqlConnection conn = new SqlConnection();
            conn.ConnectionString=ConfigurationSettings.AppSettings["ConnectionString"].ToString().Trim();
           // SqlDataReader dtrt_dic;
            DataSet ds = new DataSet();
            //string strSql = "select c_name from (select c_code,c_name from t_dic where c_code  <>'" + state + "') as a where c_code like '" + state.Substring(0,2) + "____'";
            string strSql = "select c_name from (select c_code,c_name from t_dic where c_code  <> '" + state + "') as a where c_code like '" + state.Substring(0, 2) + "____'";
            adapter.SelectCommand = new SqlCommand(strSql);        
            adapter.SelectCommand.Connection = conn;
            adapter.SelectCommand.Connection.Open();
            adapter.Fill(ds);
            adapter.SelectCommand.Connection.Close();
            XmlTextWriter w = new XmlTextWriter(this.Response.OutputStream, Response.ContentEncoding);
            w.Formatting = Formatting.Indented;            
            w.Indentation = 4;
            w.IndentChar=' ';
            ds.WriteXml(w);
            w.Flush();
            this.Response.End();
            w.Close();
        }
    }
}
这样就可以!

数据库里数据的存放是采用字典表的方式,即:事先约定120000是辽宁省,120100是沈阳市,这样使用模糊查询可以

遍历出来省市,并绑定到ddl控件!

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值