日期:2014-05-18  浏览次数:20757 次

DropDownList的联动问题
有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();[color=#FF0000][/color]//Person和Mail 为自定义的数据库操作类,返回
  [color=#FF0000][/color]//Dataset
  this.DropDownList1.DataSource = check.SelectDepartment();[color=#FF0000][/color]//绑定了
  [color=#FF0000][/color]//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");[color=#FF0000][/color]//获得 [color=#FF0000][/color]//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();[color=#FF0000][/color]//重新绑定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

请问问题出在哪了?

------解决方案--------------------
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, "请选择");