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

两个dropdownlist联动后,如何获取选中的项

以下是绑定的代码,如何才能获取选中后的值,请大家指点我一下,谢谢,我是新手


using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Data.SqlClient;

namespace WebApplication5
{
  public partial class add : System.Web.UI.Page
  {

  string strCon = "Data Source=(local);Database=LOGINDB;Uid=sa;Pwd=sa123456";


  protected void Page_Load(object sender, EventArgs e)
  {

  if (!IsPostBack) 
  { 
  //绑定ddlxueli 
  SqlConnection con = new SqlConnection(strCon); 
  con.Open(); 
  SqlCommand cmd = new SqlCommand("select * from class ", con); 
  SqlDataReader reader = cmd.ExecuteReader(); 
  ddlxueli.DataSource = reader; 
  ddlxueli.DataTextField = "classname"; 
  ddlxueli.DataValueField = "classno"; 
  ddlxueli.DataBind(); 
  reader.Close();

  //绑定DropDownList1 
  SqlCommand cmd2 = new SqlCommand("select * from deviceinfo where classno=" + this.ddlxueli.SelectedValue, con);
  reader = cmd2.ExecuteReader();//上面定义过reader,这里就不用从新定义 
  DropDownList1.DataSource = reader;
  DropDownList1.DataTextField = "deviceno";
  DropDownList1.DataValueField = "classno";
  DropDownList1.DataBind();
  reader.Close();

   
   
  } 
   

  }

  protected void btnRegion_Click(object sender, EventArgs e)
  {
  
  }

   
   
  protected void ddlxueli_SelectedIndexChanged(object sender, EventArgs e)
  {
  //DropDownList1与DropDownList2数据绑定的联动性 
  string code = this.ddlxueli.SelectedValue;
  SqlConnection con = new SqlConnection(strCon);
  con.Open();
  SqlCommand cmd = new SqlCommand("select * from deviceinfo where classno=" + code, con);
  SqlDataReader reader = cmd.ExecuteReader();
  this.DropDownList1.DataSource = reader;
  this.DropDownList1.DataTextField = "deviceno";
  this.DropDownList1.DataValueField = "classno";
  this.DropDownList1.DataBind();
  reader.Close();
  con.Close(); 

  }

  protected void txt_TextChanged(object sender, EventArgs e)
  {

  }

   
  }
}


------解决方案--------------------
SelectedIndex
------解决方案--------------------
DropDownList1.SelectedIndex就可以获取到你选定的是哪个啊
------解决方案--------------------
SelectedValue