前台页面:<asp:DropDownList ID="dropProvince" runat="server" onselectedindexchanged="dropProvince_SelectedIndexChanged" AutoPostBack="true">
          </asp:DropDownList>
          <asp:DropDownList ID="dropCity" runat="server">
          </asp:DropDownList> 后台代码:protected void Page_Load(object sender, EventArgs e)
        {
            
            if (!IsPostBack)
            {
                bindGroup();
                bind();
                string action = Request.QueryString["action"];
                if (action != null)
                {                    
                        Button_Save.Text = "保存修改";
                        if (action.Equals("modify"))
                        {
                            string ID = Request.QueryString["ID"];
                            if (ID == null || ID.Equals(""))
                            {
                                PageInfo("参数错误", "Company_List.aspx");
                            }
                            DataTable dtcompany = Tab_Company.GetTab_Company(ID);
                            if (dtcompany != null && dtcompany.Rows.Count > 0)
                            {
                                
                                this.dropProvince.SelectedValue =dtcompany.Rows[0]["ProvinceID"].ToString();
                                this.dropCity.SelectedValue =dtcompany.Rows[0]["CityID"].ToString();
                                
                            }
                        }
                    }
                }
            }           protected void Button_Save_Click(object sender, EventArgs e)
        {
            if (Page.IsValid)
            {
                try
                {
                    
                    companyenti.ProvinceID =int.Parse(this.dropProvince.SelectedValue);
                    companyenti.CityID =int.Parse(this.dropCity.SelectedValue);
                    if (((Button)sender).Text == "保存")
                    {                        if (Tab_Company.Add(companyenti) == 1)
                        {
                            PageInfo("添加成功", "Company_List.aspx");
                        }
                        else
                        {
                            PageInfo("添加失败", "Company_Add.aspx");
                        }
                    }
                    else if (((Button)sender).Text == "保存修改")
                    {
                        string ID = Request.QueryString["ID"];
                        
                        if (ID == null || ID.Equals(""))
                        {
                            PageInfo("参数错误", "Company_Add.aspx");
                        }
                        else
                        {
                            companyenti.ID = int.Parse(ID);
                            if (Tab_Company.Edit(companyenti) == 1)
                            {
                                PageInfo("修改成功", "Company_List.aspx");
                            }
                            else
                            {
                                PageInfo("修改失败", "Company_List.aspx");
                            }
                        }
                    }
                }
                catch (Exception ex)
                {
                    log.Error(ex.Message);
                }
            }
        }        /// <summary>
        /// 绑定根节点
        /// </summary>
        public void bindGroup()
        {
            string sql = "select * from Tab_Province";
            DataTable dtmenu = Tab_Province.GetByWhere(sql);
            this.dropProvince.DataSource = dtmenu;
            this.dropProvince.DataTextField = "province";
            this.dropProvince.DataValueField = "proID";
            this.dropProvince.DataBind();
        }        public void bind()
        {
            bindGroup();
            string sql = "select * from Tab_City where proID = '" + this.dropProvince.SelectedValue + "'";
            DataTable dtmenu = Tab_City.GetByWhere(sql);
            this.dropCity.DataSource = dtmenu;
            this.dropCity.DataTextField = "city";
            this.dropCity.DataValueField = "cityID";
            this.dropCity.DataBind();            
        }
        
        protected void dropProvince_SelectedIndexChanged(object sender, EventArgs e)
        {            
            string sql = "select * from Tab_City where proID = '" + this.dropProvince.SelectedValue + "'";
            DataTable dtmenu = Tab_City.GetByWhere(sql);
            this.dropCity.DataSource = dtmenu;
            this.dropCity.DataTextField = "city";
            this.dropCity.DataValueField = "cityID";
            this.dropCity.DataBind();
            
        }在修改页面能读取到省份的值,市的值也能读取到,但不是与省份对应的正确值!

解决方案 »

  1.   

     protected void dropProvince_SelectedIndexChanged(object sender, EventArgs e)
      {   
      string sql = "select * from Tab_City where proID = '" + this.dropProvince.SelectedValue + "'";
      DataTable dtmenu = Tab_City.GetByWhere(sql);
      this.dropCity.DataSource = dtmenu;
      this.dropCity.DataTextField = "city";
      this.dropCity.DataValueField = "cityID";
      this.dropCity.DataBind();
        
      }
    你看看这边有没有查出数据
      

  2.   

    是不是这的查询语句写的不对?
    public void bind()
      {
      bindGroup();
      string sql = "select * from Tab_City where proID = '" + this.dropProvince.SelectedValue + "'";
      DataTable dtmenu = Tab_City.GetByWhere(sql);
      this.dropCity.DataSource = dtmenu;
      this.dropCity.DataTextField = "city";
      this.dropCity.DataValueField = "cityID";
      this.dropCity.DataBind();  
      }
      

  3.   

    string sql = "select * from Tab_City where proID = '" + this.dropProvince.SelectedValue + "'";
    跟踪调试一下,看你这个语句能在数据库里执行查出东西么?
      

  4.   

    this.dropProvince.SelectedValue的值是1,在数据库中能查出北京市的所有地区。但是省份如果不是北京,它的值还是那样,不变!
      

  5.   

    下拉框的值的绑定一定要在(!IsPostBack)里绑定好。