有2个DropDownLis,分别为DropDownLis1,DropDownLis2,实现的效果是选择DropDownLis1的项,DropDownLis2相应的变化
代码片断如下protected void Page_Load(object sender, EventArgs e)
    {
        if (this.IsPostBack != true)
        {
            Person check = new Person();
            Mail STName = new Mail();//Person和Mail 为自定义的数据库操作类,返回
                                           //Dataset
            this.DropDownList1.DataSource = check.SelectDepartment();//绑定了
                                                                    //DropDownLis1的数据
            this.DropDownList1.DataTextField = "DepName";
            this.DropDownList1.DataValueField = "DepID";
            this.DropDownList1.DataBind();
            
            string Dep = this.DropDownList1.SelectedItem.Text.ToString();
            this.DropDownList2.DataSource = STName.SentToName("Dep");//获得 //DropDownLis1选择的项,做为参数已获取DropDownLis2的数据源
            this.DropDownList2.DataTextField = "RealName";
            this.DropDownList2.DataValueField = "ID";
            this.DropDownList2.DataBind();
            this.DataBind();
            //this.DropDownList1.Items.Insert(0, "请选择");
            
        }
    }    protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
    {
        Mail STName = new Mail();//重新绑定DropDownLis2的数据
        string Dep = this.DropDownList1.SelectedItem.Text.ToString();
        this.DropDownList2.DataSource = STName.SentToName("Dep");
        this.DropDownList2.DataTextField = "RealName";
        this.DropDownList2.DataValueField = "ID";
        this.DropDownList2.DataBind();
        this.DropDownList2.Items.Insert(0, "请选择");
    }
给DropDownList2绑定数据源的存储过程如下:ALTER PROCEDURE Person_Mail_STName

@Dep nvarchar(50)
AS

begin
SELECT    RealName,Staff.ID
FROM         Staff INNER JOIN
                      Department ON Staff.DepID = Department.DepID
WHERE     (Department.DepName = @Dep)
end结果是DropDownLis1可以正确显示数据,但是DropDownLis2没有数据,DropDownLis1的AUTOPOSTBACK已设置为True请问问题出在哪了?

解决方案 »

  1.   

      protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e) 
        { 
            Mail STName = new Mail();//重新绑定DropDownLis2的数据 
            string Dep = this.DropDownList1.SelectedItem.Text.ToString();         this.DropDownList2.DataSource = STName.SentToName("Dep");//Dep为何加引号?
     
        
            this.DropDownList2.DataTextField = "RealName"; 
            this.DropDownList2.DataValueField = "ID"; 
            this.DropDownList2.DataBind(); 
            this.DropDownList2.Items.Insert(0, "请选择"); 
        } 
      

  2.   


     protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e) 
        { 
            Mail STName = new Mail();//重新绑定DropDownLis2的数据 
            string Dep = this.DropDownList1.SelectedItem.Text.ToString(); 
            \\this.DropDownList2.DataSource = STName.SentToName("Dep"); 
            this.DropDownList2.DataSource = STName.SentToName(Dep); 
            this.DropDownList2.DataTextField = "RealName"; 
            this.DropDownList2.DataValueField = "ID"; 
            this.DropDownList2.DataBind(); 
            this.DropDownList2.Items.Insert(0, "请选择"); 
        } 
      

  3.   

    string Dep = this.DropDownList1.SelectedItem.Text.ToString(); 
    this.DropDownList2.DataSource = STName.SentToName("Dep"); "Dep"的引號去掉.
    this.DropDownList2.DataSource = STName.SentToName(Dep);