//获取文本
this.DropDownList1.SelectedItem.Text;
本类内使用
protected void Page_Load(object sender, EventArgs e)
{
this.DropDownList1.Items.Clear();
this.DropDownList1.DataSource = DbHelperSQL.Query("select * from authors").Tables[0];
this.DropDownList1.DataTextField = "au_id";
this.DropDownList1.DataValueField = "au_lname";
this.DropDownList1.DataBind(); text value
this.DropDownList1.Items.Insert(0,new ListItem("",""));//插入空项,此举必须放到数据绑定之后
}
别忘了要判断isPostBack
//事件selectIndexChangeing
DropDownList的Items包括ListItem对象,ListItem有Value和Text两个属性,
取值取的是value不是text this.DropDownList1.SelectValue;//value
this.DropDownList1.SelectValue://取当前文本框中所显示的键值
//公共方法
///
/// DropDownList绑定数据库
///
/// 本窗体上的dropDownList控件
/// 如:Db.Query("select * from tbtype")
/// dropDownList上显示的text文本值(数据库中的name)
/// value值(对应数据库中的id)
/// dropdownlist首先显示的文本值(数据库中的name)
/// firstText对应的键(数据库中的id)
public static void BindDropDownList(object dropDownList, DataSet ds, string text, string Value, string firstText, string firstValue)
{
if (dropDownList == null || ds == null)
{
return;
}
if (dropDownList is System.Web.UI.WebControls.DropDownList)
{
((System.Web.UI.WebControls.DropDownList)dropDownList).Items.Clear();
((System.Web.UI.WebControls.DropDownList)dropDownList).DataTextField = text;
((System.Web.UI.WebControls.DropDownList)dropDownList).DataValueField = Value;
((System.Web.UI.WebControls.DropDownList)dropDownList).DataSource = ds.Tables[0];
((System.Web.UI.WebControls.DropDownList)dropDownList).DataBind();
((System.Web.UI.WebControls.DropDownList)dropDownList).Items.Insert(0, new System.Web.UI.WebControls.ListItem(firstText, firstValue));//dropdownlist首次显示的text,value
//if (((DropDownList)dropDownList).Items.Count > 0)
//{
// ((DropDownList)dropDownList).SelectedIndex = 0;//首先显示第一项
//}
}
}
-------------------------
上面方法重载,参数中没有“全部”,“全部”,即首次显示的不是”全部“
///
/// DropDownList绑定数据库
///
/// 本窗体上的dropDownList控件
/// 如:Db.Query("select * from tbtype")
/// dropDownList上显示的text文本值(数据库中的name)
/// value值(对应数据库中的id)
/// dropdownlist首先显示的文本值(数据库中的name)
/// firstText对应的键(数据库中的id)
public static void BindDropDownList(object dropDownList, DataSet ds, string text, string Value)
{
if (dropDownList == null || ds == null)
{
return;
}
if (dropDownList is System.Web.UI.WebControls.DropDownList)
{
((System.Web.UI.WebControls.DropDownList)dropDownList).Items.Clear();
((System.Web.UI.WebControls.DropDownList)dropDownList).DataTextField = text;
((System.Web.UI.WebControls.DropDownList)dropDownList).DataValueField = Value;
((System.Web.UI.WebControls.DropDownList)dropDownList).DataSource = ds.Tables[0];
((System.Web.UI.WebControls.DropDownList)dropDownList).DataBind();
//((System.Web.UI.WebControls.DropDownList)dropDownList).Items.Insert//(0, new System.Web.UI.WebControls.ListItem(firstText, firstValue));//dropdownlist首次显示的text,value
//if (((DropDownList)DropDownList).Items.Count > 0)
//{
// ((DropDownList)DropDownList).SelectedIndex = 0;
//}
}
}
//调用时要判断isPostBack
---------------------------------------------------------------
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
PublicClass.BindDropDownList(this.DropDownList1, Db.Query("select * from BookType"), "BookTypeName", "BookTypeId", "全部", "全部");
}
}
--------------------------------------------------------------------
//添加项
DataTable dt = Spbase.GreatDs("Select * from Spclass").Tables[0];
for (int i = 0; i < dt.Rows.Count; i++)
{
ListItem Dr = new ListItem(dt.Rows[i]["name"].ToString(), dt.Rows[i][0].ToString());
DropDownList1.Items.Add(Dr);
}