首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 网站开发 > asp.net >

DropDownList和GridView联动有关问题

2012-08-02 
DropDownList和GridView联动问题我的GrideView就是直接空间拖的 没有模板列,现在的问题是ddl1选择的时候gv

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.Open();            SqlCommand sqlcom = new SqlCommand();            sqlcom.Connection = sqlcon;            sqlcom.CommandText = "select eID from e_List";            sqlcom.CommandType = CommandType.Text;            SqlDataAdapter sqlda = new SqlDataAdapter(sqlcom);            ds = new DataSet();            sqlda.Fill(ds);            DropDownListBranch.DataSource = ds.Tables[0];            DropDownListBranch.DataTextField = "eID";            DropDownListBranch.DataValueField = "eID";            DropDownListBranch.DataBind();        }        catch (Exception ex)        {            throw (ex);        }        finally        {            sqlcon.Close();        }    }    private void BindSupport()    {        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 ID from List";            sqlcom.CommandType = CommandType.Text;            SqlDataAdapter sqlda = new SqlDataAdapter(sqlcom);            ds = new DataSet();            sqlda.Fill(ds);            DropDownListSupport.DataSource = ds.Tables[0];            DropDownListSupport.DataTextField = "ID";            DropDownListSupport.DataValueField = "ID";            DropDownListSupport.DataBind();        }        catch (Exception ex)        {            throw (ex);        }        finally        {            sqlcon.Close();        }    }    protected void Page_Load(object sender, EventArgs e)    {        if (!IsPostBack)        {            BindData();            BindLine();            BindSupport();            DropDowntID.Items.Insert(0, new ListItem("-请选择-", "0"));            DropDowntID.SelectedIndex = 0;            DropDownListBranch.Items.Insert(0, new ListItem("-请选择-", "0"));            DropDownListBranch.SelectedIndex = 0;            DropDownListSupport.Items.Insert(0, new ListItem("-请选择-", "0"));            DropDownListSupport.SelectedIndex = 0;        } 

热点排行