XmlHttp实现无刷新三联动下拉框

SCRIPT LANGUAGE="JavaScript">
            <!--
                //以XML求取DropDownList2的数据
                function XmlPost2(obj)
                {
                  var svalue = obj.value;
                  var webFileUrl = "?povinceid=" + svalue;
                  var result = "";
                  var xmlHttp = new ActiveXObject("MSXML2.XMLHTTP");
                  xmlHttp.open("POST", webFileUrl, false);
                  xmlHttp.send("");
                  result = xmlHttp.responseText;
                  
                  if(result != "")
                  {
                    document.all("DropDownList2").length=0;
                    var piArray = result.split(",");
                    for(var i=0;i<piArray.length;i++)
                    {
                      var ary1 = piArray[i].toString().split("|");
                      document.all("DropDownList2").options.add(new Option(ary1[1].toString(),ary1[0].toString()));
                    }
                  }
                  else
                  {
                    alert(result);
                  }
                }
                //以XML求取DropDownList3的数据
                function XmlPost3(obj)
                {
                  var svalue = obj.value;
                  var webFileUrl = "?cityid=" + svalue;
                  var result = "";
                  var xmlHttp = new ActiveXObject("MSXML2.XMLHTTP");
                  xmlHttp.open("POST", webFileUrl, false);
                  xmlHttp.send("");
                  result = xmlHttp.responseText;
                  
                  if(result != "")
                  {
                    document.all("DropDownList3").length=0;
                    var piArray = result.split(",");
                    for(var i=0;i<piArray.length;i++)
                    {
                      var ary1 = piArray[i].toString().split("|");
                      document.all("DropDownList3").options.add(new Option(ary1[1].toString(),ary1[0].toString()));
                    }
                  }
                  else
                  {
                    alert(result);
                  }
                }
                function getData()
                {
                    var province=document.getElementById("DropDownList1");
                    var pindex = province.selectedIndex;
                    var pValue = province.options[pindex].value;
                    var pText  = province.options[pindex].text;
                   
                    var city=document.getElementById("DropDownList2");
                    var cindex = city.selectedIndex;
                    var cValue = city.options[cindex].value;
                    var cText  = city.options[cindex].text;
                   
                    var area=document.getElementById("DropDownList3");
                    var aindex = area.selectedIndex;
                    var aValue = area.options[aindex].value;
                    var aText  = area.options[aindex].text;
                   
                    var txt=document.getElementById("TextBox1");                               

                    document.getElementById("<=TextBox1.ClientID>").innerText="省:"+pValue+"|"+pText+"市:"+cValue+"|"+cText+"区:"+aValue+"|"+aText;
                    document.Form1.hidprovince.value=pValue;
                    document.Form1.hidcity.value=cValue;
                    document.Form1.hidarea.value=aValue;
                }
            //-->
            </SCRIPT>
        </form>
    </body>
</HTML>

2.cs代码

public class WebForm1 : System.Web.UI.Page
    {
        protected System.Web.UI.WebControls.DropDownList DropDownList1;
        protected System.Web.UI.WebControls.DropDownList DropDownList2;
        protected System.Web.UI.WebControls.DropDownList DropDownList3;
        protected System.Web.UI.WebControls.TextBox TextBox1;
   
        public static string ConnectionString=System.Configuration .ConfigurationSettings .AppSettings["ConnectionString"];
   
        GetDataSet#region GetDataSet
        public static DataSet GetDataSet(string sql)
        {
            SqlDataAdapter    sda =new SqlDataAdapter(sql,ConnectionString);
            DataSet ds=new DataSet();
            sda.Fill(ds);
            return ds;
        }
        #endregion

        property#region property
        private string provinceid
        {
            get
            {
                if(ViewState["provinceid"]!=null && ViewState["provinceid"].ToString()!="")
                {
                    return ViewState["provinceid"].ToString();
                }
                else
                {
                    if(Request["provinceid"]!=null && Request["provinceid"].ToString()!="")
                    {
                        return Request["provinceid"];
                    }
                    else
                    {
                        return "";
                    }
                }
            }
            set
            {
                ViewState["provinceid"]=value;
            }
        }
        private string cityid
        {
            get
            {
                if(ViewState["cityid"]!=null && ViewState["cityid"].ToString()!="")
                {
                    return ViewState["cityid"].ToString();
                }
                else
                {
                    if(Request["cityid"]!=null && Request["cityid"].ToString()!="")
                    {
                        return Request["cityid"];
                    }
                    else
                    {
                        return "";
                    }
                }
            }
            set
            {
                ViewState["cityid"]=value;
            }
        }
        #endregion

        Page_Load#region Page_Load
        private void Page_Load(object sender, System.EventArgs e)
        {
            if(!this.IsPostBack)
          {
            this.down1_bind();
                this.DropDownList1.Attributes.Add("onchange","XmlPost2(this);");
                this.DropDownList2.Attributes.Add("onchange","XmlPost3(this);");
          }
          if(provinceid != "")
          {
            this.down2_bind(provinceid);
          }
            if(cityid != "")
          {
            this.down3_bind(cityid);
          }
        }
       
        #endregion

        down2_bind#region down2_bind
        private void down2_bind(string id)
        {
          string mystr = "";
          string sql = "select cityID,city from city where father = '" + id + "'";
          DataSet ds = GetDataSet(sql);

          if(ds.Tables[0].Rows.Count != 0)
          {
            for(int i=0;i<ds.Tables[0].Rows.Count;i++)
            {
              mystr += "," + ds.Tables[0].Rows[i][0].ToString() + "|" + ds.Tables[0].Rows[i][1].ToString();
            }
            mystr = mystr.Substring(1);
          }
          this.Response.Write(mystr);
          this.Response.End();
        }
        #endregion

        down3_bind#region down3_bind
        private void down3_bind(string id)
        {
          string mystr = "";
          string sql = "select areaID,area from area where father = '" + id + "'";
          DataSet ds = GetDataSet(sql);

          if(ds.Tables[0].Rows.Count != 0)
          {
            for(int i=0;i<ds.Tables[0].Rows.Count;i++)
            {
              mystr += "," + ds.Tables[0].Rows[i][0].ToString() + "|" + ds.Tables[0].Rows[i][1].ToString();
            }
            mystr = mystr.Substring(1);
          }
          this.Response.Write(mystr);
          this.Response.End();
        }

        #endregion
       
        down1_bind#region down1_bind
        private void down1_bind()
        {
          string sql = "select provinceID,province from province";
          DataSet ds = GetDataSet(sql);
          this.DropDownList1.DataSource = ds;
          this.DropDownList1.DataValueField = "provinceID";
          this.DropDownList1.DataTextField = "province";
          this.DropDownList1.DataBind();         
        }

        #endregion

        Web Form Designer generated code#region Web Form Designer generated code
        override protected void OnInit(EventArgs e)
        {
            //
            // CODEGEN: This call is required by the ASP.NET Web Form Designer.
            //
            InitializeComponent();
            base.OnInit(e);
        }

        /**//// <summary>
        /// Required method for Designer support - do not modify
        /// the contents of this method with the code editor.
        /// </summary>
        private void InitializeComponent()
        {   
            this.Load += new System.EventHandler(this.Page_Load);

        }
        #endregion
    }

展开阅读全文

xmlhttp实现无刷新联动listbox

09-07

前台代码:rnrnrnrnrn 无标题页rn rnrnrn rn rn rn rn rn rn rn rn rn rn rn rn rn rnrnrnrn后台代码:rn#region 页面加载rn protected void Page_Load(object sender, EventArgs e)rn rn if (!IsPostBack)rn rn //加载游戏名称rn LoadGameName();rn this.lb_GameName.Attributes.Add("onchange", "XmlPost2(this);");rn this.lb_GameType.Attributes.Add("onchange", "XmlPost3(this);");rn rn if (GNId != "")rn rn LoadGameType(Convert.ToInt32(GNId));rn rn if (GTId != "")rn rn LoadGameZone(Convert.ToInt32(GTId));rn rn rn #endregionrnrn //游戏Idrn private string GNIdrn rn getrn rn if (ViewState["GNId"] != null && ViewState["GNId"].ToString() != "")rn rn return ViewState["GNId"].ToString();rn rn elsern rn if (Request["GNId"] != null && Request["GNId"].ToString() != "")rn rn return Request["GNId"];rn rn elsern rn return "";rn rn rn rn setrn rn ViewState["GNId"] = value;rn rn rnrn //游戏类型IDrn private string GTIdrn rn getrn rn if (ViewState["GTId"] != null && ViewState["GTId"].ToString() != "")rn rn return ViewState["GTId"].ToString();rn rn elsern rn if (Request["GTId"] != null && Request["GTId"].ToString() != "")rn rn return Request["GTId"];rn rn elsern rn return "";rn rn rn rn setrn rn ViewState["GTId"] = value;rn rn rnrnrn rn #region 加载游戏名称rn /// rn /// 加载游戏名称rn /// rn private void LoadGameName()rn rn DataTable dt = AdminServices.SP_select("GameName", "GNId,GNName");rn if (dt.Rows.Count > 0)rn rn this.lb_GameName.DataSource = dt;rn this.lb_GameName.DataTextField = "GNName";rn this.lb_GameName.DataValueField = "GNId";rn this.lb_GameName.DataBind();rn rnrn rn #endregionrnrn //加载游戏所属分类rn private void LoadGameType(int GNId)rn rn string mystr = "";rn DataTable dt = AdminServices.S_selectOne("V_Gtype", "GTId,T2Name,T2Id", " GNId=" + GNId);rn if (dt.Rows.Count > 0)rn rn for (int i = 0; i < dt.Rows.Count; i++)rn rn mystr += "," + dt.Rows[i][0].ToString() + "|" + dt.Rows[i][1].ToString();rn rn mystr = mystr.Substring(1);rn rn this.Response.Write(mystr);rn this.Response.End();rn rn //加载游戏所属区域rn private void LoadGameZone(int GTId)rn rn string mystr = "";rn DataTable dt = AdminServices.S_selectOne("V_GameZone", "GZId,ZName,ZID", " GTId=" + GTId);rn if (dt.Rows.Count > 0)rn rn for (int i = 0; i < dt.Rows.Count; i++)rn rn mystr += "," + dt.Rows[i][0].ToString() + "|" + dt.Rows[i][1].ToString();rn rn mystr = mystr.Substring(1);rn rn this.Response.Write(mystr);rn this.Response.End();rn rn override protected void OnInit(EventArgs e)rn rn //rn // CODEGEN: This call is required by the ASP.NET Web Form Designer.rn //rn InitializeComponent();rn base.OnInit(e);rn rnrn /**/rn /// rn /// Required method for Designer support - do not modifyrn /// the contents of this method with the code editor.rn /// rn private void InitializeComponent()rn rn this.Load += new System.EventHandler(this.Page_Load);rnrn rnrn页面运行时,依然刷新,本来是要实现无刷新效果的rn 论坛

没有更多推荐了,返回首页