如题!采用 if (DropDownList2.SelectedItem.Text!="请选择")
{
shi=" and city like '%"+DropDownList2.SelectedItem.Text +"%'";
}
方式取不到第二个和第三个框的文本值,而且第一次加载时选取第一个框的时候还要停顿十秒钟!
前台代码:
<script language="javascript">
<!--
//以XML求取数据
//参数:
//obj,当前动作的下拉框
//fullName:要填充的下拉框
function XmlPost(obj,fullName,sType)
{
//求取当前下拉框的值
   var svalue = obj.value;
  
   var mytopname = "";
   if(sType == "1")
   {
   mytopname = "请选择";
   }
   else if(sType == "2")
   {
   mytopname = "请选择";
   }
   else
   {
   mytopname = "请选择";
   }
  
   //定义要填充的对象
   var fullObj = document.all(fullName);
  
   //定义取值地址
   var webFileUrl = "?s1=" + svalue + "&s2=" + sType;
  
   //定义返回值
   var result = "";
  
   //开始取值过程
   var xmlHttp = new ActiveXObject("MSXML2.XMLHTTP");
   xmlHttp.open("POST", webFileUrl, false);
   xmlHttp.send("");
   result = xmlHttp.responseText;   //如果有取到值,则根据格式进行拆分
  //注意如果选择了第一行"请选择"空行时,是取不到值的,因此多加了一个条件
   if(result != "" && svalue != "")
   {
   //先清空原有的值
     fullObj.length=0;
    
     fullObj.options.add(new Option(mytopname,""));    
    
     //拆分值成数组
     var piArray = result.split(",");
    
     //循环数组
     for(var i=0;i<piArray.length;i++)
     {
     //再拆分成ID及名称
       var ary1 = piArray[i].toString().split("|");
       //逐一添加项
       fullObj.options.add(new Option(ary1[1].toString(),ary1[0].toString()));
     }
   }
   else
   {
     //如果没有取到值,则清空要填充的下拉框的值
     fullObj.length = 0;
     fullObj.options.add(new Option(mytopname,""));
   }
  
   //如果是选择了第一个下拉框,此时应清空第三个下拉框内容
   if(sType == "1")
   {
   document.all("DropDownList3").length = 0;
     document.all("DropDownList3").options.add(new Option(mytopname,""));
   } }
-->
</script>

解决方案 »

  1.   

    private void Page_Load(object sender, System.EventArgs e)
    {

    string sortid = this.Request.QueryString["s1"];
    string sType = this.Request.QueryString["s2"];
    if(sortid + "a" != "a")
    {
    // //如果有传递上述参数,则表示联动操作开始
    this.xmlBind(sortid,sType);
    }
    if(!this.IsPostBack)
    {
    hangye();
    PRBind();
    // TextBox2.Text=Request.Params["gg"];
    bq=Request.Params["bq"];
    jck=Request.Params["jck"]; this.DownBind1();
    this.DownBind2();
    this.DownBind3();
      

  2.   

    }

    DropDownList1.Attributes.Add("onBlur", ""+this.Hidden1.ClientID+".value=this.options[this.selectedIndex].value");
    DropDownList2.Attributes.Add("onBlur", ""+this.Hidden2.ClientID+".value=this.options[this.selectedIndex].value");
    DropDownList3.Attributes.Add("onBlur", ""+this.Hidden3.ClientID+".value=this.options[this.selectedIndex].value");

    }
    private void xmlBind(string sortid,string sType)
    {
    string mystr = "";
    string sql = "";
    string gc;
    if(sType == "1")
    {
    //Labely.Text="1";
    Hidden2.Value="";Hidden3.Value="";
    gc=sortid.Substring(0,2);
    sql ="SELECT DISTINCT 行政区代码 as id,名称1 as sname FROM 行政编码 WHERE (名称1 IS NOT NULL) AND (名称2 IS NULL) AND (行政区代码 like '" + gc +"%')";
    }
    else
    {
    Hidden3.Value="";
    gc=sortid.Substring(0,4);
    sql = "SELECT DISTINCT 行政区代码 as id,名称2 as sname FROM 行政编码 WHERE (名称2 IS NOT NULL) AND (行政区代码 like '" + gc+"%')";
    } DataTable mytab = this.Get_Dt(sql); //将取到的值形成: ID|名称,ID|名称...这样的形式
    if(mytab.Rows.Count != 0)
    {
    for(int i=0;i<mytab.Rows.Count;i++)
    {
    mystr += "," + mytab.Rows[i]["id"].ToString() + "|" + mytab.Rows[i]["sname"].ToString();
    }
    mystr = mystr.Substring(1);
    }
    //输出页面
    this.Response.Write(mystr);
    this.Response.End();
    } /// <summary>
    /// 根据SQL语句返回数据集DataTable
    /// </summary>
    /// <param name="sql">要产生数据集的SQL语句</param>
    /// <returns>返回DataTable</returns>
    private DataTable Get_Dt(string sql)
    {
    SqlConnection conn=new SqlConnection();
    //数据库连接串,本数据库为ACCESS数据库,当前在本目录的根目录下
    string connString = System.Configuration.ConfigurationSettings .AppSettings["ConnSql"];
    conn = new SqlConnection(connString);
    //打开数据库
    conn.Open();

    SqlDataAdapter myAdp = new SqlDataAdapter(sql,conn);
    DataTable myDt = new DataTable();

    try
    {
    //填充数据
    myAdp.Fill(myDt);
    //返回数据集
    return(myDt);
    }
    catch(SqlException ex)
    {
    //显示错误信息
    throw ex;
    }
    finally
    {
    //关闭数据库连接
    conn.Close();
    }
    } /// <summary>
    /// 绑定第一个下拉框
    /// </summary>
    private void DownBind1()
    {
    //显示所有的主分类
    string sql = "SELECT DISTINCT 行政区代码,名称 FROM 行政编码 WHERE 名称1 IS NULL";
    DataTable mytab = this.Get_Dt(sql); //绑定第一个下拉框
    this.DropDownList1.DataSource = mytab;
    this.DropDownList1.DataValueField = "行政区代码";
    this.DropDownList1.DataTextField = "名称";
    this.DropDownList1.DataBind(); //添加一个"请选择"行
    this.DropDownList1.Items.Insert(0,new ListItem("请选择省别","")); //为此下拉框添加一个默认选择项,选中第2个选项
    if(this.DropDownList1.Items.Count > 1)
    {
    this.DropDownList1.SelectedIndex =0;
    } //为此下拉框添加选择事件,第一个参数是自己
    //第二个参数为要填充的下拉框的名称 
    //第三个参数为求取类型
    this.DropDownList1.Attributes.Add("onchange","XmlPost(this,'" + this.DropDownList2.ClientID + "','1');"); }
    /// <summary>
    /// 绑定第二个下拉框
    /// </summary>
    private void DownBind2()
    {
    string sValue = this.DropDownList1.SelectedValue.ToString();

    //string sValue = this.DropDownList1.SelectedItem.Text; //为第二个下拉框添加事件
    this.DropDownList2.Attributes.Add("onchange","XmlPost(this,'" + this.DropDownList3.ClientID + "','2');"); //第一个下拉框有值才开始绑定第二个下拉框
    if(sValue != "")
    {
    sValue=sValue.Substring(0,2);
    //默认显示分类号为1的所有子类
       string sql = "SELECT 行政区代码,名称1 FROM 行政编码 WHERE (名称1 IS NOT NULL) AND (名称2 IS NULL) AND (行政区代码 like '" + sValue+"%')" ;
       DataTable mytab = this.Get_Dt(sql); //绑牢控件
       this.DropDownList2.DataSource = mytab;
       this.DropDownList2.DataSource = mytab;
       this.DropDownList2.DataValueField = "行政区代码";
       this.DropDownList2.DataTextField = "名称1";
       this.DropDownList2.DataBind();
    } //添加一个空的首行
    this.DropDownList2.Items.Insert(0,new ListItem("请选择市别","")); //如果有选项,则选中它
    if(this.DropDownList2.Items.Count > 1)
    {
    this.DropDownList2.SelectedIndex =0;
    }

    }
    /// <summary>
    /// 绑定第三个下拉框
    /// </summary>
    private void DownBind3()
    {
    string sValue = this.DropDownList2.SelectedValue.ToString();

    //第一个下拉框有值才开始绑定第二个下拉框
    if(sValue != "")
    {
    sValue=sValue.Substring(0,4);
    //默认显示分类号为1的所有子类
       string sql = "SELECT 行政区代码,名称2 FROM 行政编码 WHERE (名称2 IS NOT NULL) AND (行政区代码 like'" + sValue+"%')";
       DataTable mytab = this.Get_Dt(sql); //绑牢控件
       this.DropDownList3.DataSource = mytab;
       this.DropDownList3.DataSource = mytab;
       this.DropDownList3.DataValueField = "行政区代码";
       this.DropDownList3.DataTextField = "名称2";
       this.DropDownList3.DataBind(); } //添加一个空的首行
    this.DropDownList3.Items.Insert(0,new ListItem("请选择县区",""));
    //如果有选项,则选中它
    if(this.DropDownList3.Items.Count > 1)
    {
    this.DropDownList3.SelectedIndex =0;
    }

    }
    public void online()
    {
    SqlConnection conn=new SqlConnection(ConfigurationSettings.AppSettings["read7"]);
    conn.Open();
    SqlCommand cmd=new SqlCommand();
    cmd.CommandText="SELECT 在线用户.coid,companytable.* FROM 在线用户 INNER JOIN companytable ON 在线用户.coid = companytable.Id where 在线用户.coid<>2111031000025 and left(companytable.行业编码,1)='"+ Request.Params["leibie"].ToString()+"'";
    if(Request.Params["g"]!=null)
    {
    cmd.CommandText=cmd.CommandText+"and companytable.coname like'%"+Request.Params["g"]+"%'";
    }
    if(Request.Params["dq"]!=null)
    {
    cmd.CommandText=cmd.CommandText+"and companytable.地区编码 like '"+Request.Params["dq"]+"%'";
    }
    cmd.Connection=conn;
    SqlDataAdapter da=new SqlDataAdapter();
    da.SelectCommand=cmd;
    DataSet ds=new DataSet();
    int gc=da.Fill(ds);
    PagedDataSource objPage1=new PagedDataSource();

    objPage1.DataSource=ds.Tables[0].DefaultView; objPage1.AllowPaging=true;

    objPage1.PageSize=10;
    int CurPage;
    if(Request.QueryString["Page"]!=null)
    CurPage=Convert.ToInt32(Request.QueryString["Page"]);
    else CurPage=1;
    objPage1.CurrentPageIndex=CurPage-1;
    if(gc==0)
    {
    Panelon.Visible=false;
    }
    else 
    {
    Panelon.Visible=true;
    RepeaterTem1.DataSource=objPage1;
    RepeaterTem1.DataBind();
    }
    da.Dispose();
    conn.Close();
    }
    现在我能实现三级联动,就是取不到第二个和第三个文本的值,停顿就停顿吧,我也没空理它了,最近被这个三级联动搞得精疲力尽
      

  3.   

    第一个问题也搞定了,结帖,我说了半天话也没有人理我,byhum(相见不如怀念),分全给你了,谢谢啊