日期:2014-05-17  浏览次数:20555 次

DropDownList和GridView联动问题
我的GrideView就是直接空间拖的 没有模板列,现在的问题是ddl1选择的时候gv能显示数据但ddl2选择的时候,数据没有变化。。我学的J2EE~ 第一次接触。net- - 大神们指教一下啊
[code=C#][/code]
 protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
  { if (this.DropDownList1.SelectedItem.Text != "--请选择--")
  {
  string sql = "select CircID,SubsName,CircName,MainWireLength,MainWireStyle,TranCap,PeakCurrent,LimitCurrent,ConnCirc1,ConnCirc2 from VW_Circ_Subs_Region where Region= '" + this.DropDownList1.SelectedItem.Text + "'";
  this.DropDownList2.Items.Clear();
  var p = (from a in hnDataContext.VW_Circ_Subs_Region
  where a.Region.ToString() == this.DropDownList1.SelectedItem.Text
  select a.SubsName).Distinct();
  this.DropDownList2.DataSource = p;
  this.DropDownList2.Items.Add(new ListItem("--请选择--", "-1"));
  this.DropDownList2.DataBind();
  getdata(sql);


 protected void DropDownList2_SelectedIndexChanged(object sender, EventArgs e)
  {  
  if (this.DropDownList2.SelectedItem.Text != "--请选择--")
  {
  string sql = "select CircID,SubsName,CircName,MainWireLength,MainWireStyle,TranCap,PeakCurrent,LimitCurrent,ConnCirc1,ConnCirc2 from VW_Circ_Subs_Region where Region='" + this.DropDownList1.SelectedItem.Text + "' and SubsName='" + this.DropDownList2.SelectedItem.Text + "'";
  getdata(sql);
  }
  }
 protected void getdata(string sql)
  {
  DataTable dt = new DataTable();
  dt.Columns.Add("中压线路编号", typeof(System.String));
  SqlConnection conn = SD.createConn();
  SqlCommand cmd = new SqlCommand(sql, conn);
  conn.Open();
  SqlDataReader sda = cmd.ExecuteReader();
   
  while (sda.Read())
  {
  DataRow newRow = dt.NewRow();
  newRow["中压线路编号"] = sda["CircID"];
  //这里还有许多数据处理
  }
 this.GridView1.DataSource = dt;
  this.GridView1.DataBind();
  conn.Close();
  sda.Close();
  }

------解决方案--------------------
一点参考:
C# code


    private void BindData()
    {
        SqlConnection sqlcon = new SqlConnection();
        try
        {
            sqlcon.ConnectionString = System.Configuration.ConfigurationManager.ConnectionStrings["DB_CON"].ConnectionString;
            sqlcon.Open();
            SqlCommand sqlcom = new SqlCommand();
            sqlcom.Connection = sqlcon;
            sqlcom.CommandText = "select t_ID from t_List";
            sqlcom.CommandType = CommandType.Text;
            SqlDataAdapter sqlda = new SqlDataAdapter(sqlcom);
            ds = new DataSet();
            sqlda.Fill(ds);
            DropDowntID.DataSource = ds.Tables[0];
            DropDowntID.DataTextField = "t_ID";
            DropDowntID.DataValueField = "t_ID";
            DropDowntID.DataBind();
        }
        catch (Exception ex)
        {
            throw (ex);
        }
        finally
        {
            sqlcon.Close();
        }
    }

    private void BindLine()
    {
         SqlConnection sqlcon = new SqlConnection();
        try
        {
            sqlcon.ConnectionString = System.Configuration.ConfigurationManager.ConnectionStrings["DB_CON"].ConnectionString;
            sqlcon.Ope