如何获取DropDownList选中的值?
我有三个DropDownList,三个控件的AppendDataBoundItems=true,装了vs.net2005sp1了,在Page_Load中if (!IsPostBack)
{
BindList();
}
public void BindList()
{
string strSQL = "Select BigCategoryID, BigCategoryName_CH From BigCategories";
this.ddl_BigCategory.DataSource = tool.GetDataSet(strSQL);
this.ddl_BigCategory.DataValueField = "BigCategoryID";
this.ddl_BigCategory.DataTextField = "BigCategoryName_CH";
this.ddl_BigCategory.Items.Add(new ListItem(" Please select a uses ","0"));
this.ddl_BigCategory.DataBind();
strSQL = "Select CategoryID, CategoryName_CH From Categories";
this.ddl_SubCategory.DataSource = tool.GetDataSet(strSQL);
this.ddl_SubCategory.DataValueField = "CategoryID";
this.ddl_SubCategory.DataTextField = "CategoryName_CH";
this.ddl_SubCategory.Items.Add(new ListItem(" Please select a category ", "0"));
this.ddl_SubCategory.DataBind();
strSQL = "Select ModelID, Model From Models";
this.ddl_Model.DataSource = tool.GetDataSet(strSQL);
this.ddl_Model.DataValueField = "ModelID";
this.ddl_Model.DataTextField = "Model";
this.ddl_Model.Items.Add(new ListItem(" Please select a model ", "0"));
this.ddl_Model.DataBind();
}
我现在想在选中第一个单击查询的时候取它的值,
protected void btnSearch_Click(object sender, EventArgs e)
{
this.lblInfo.Text = this.ddl_BigCategory.SelectedValue;
//断点lblInfo.Text为空,
Response.Write(ddl_BigCategory.SelectedItem.Value);
//报错说Object reference not set to an instance of an object.
//BindList();
}
各位帮我看看 --------------------编程问答-------------------- this.ddl_BigCategory.DataSource = tool.GetDataSet(strSQL);
this.ddl_BigCategory.DataValueField = "BigCategoryID";
this.ddl_BigCategory.DataTextField = "BigCategoryName_CH";
this.ddl_BigCategory.Items.Add(new ListItem(" Please select a uses ","0"));
this.ddl_BigCategory.DataBind();
后面加一句
if(ddl_BigCategory.Count > 0)
ddl_BigCategory.SelectIndex = 0; --------------------编程问答-------------------- protected void btnSearch_Click(object sender, EventArgs e)
{
if(this.ddl_BigCategory.selectedIndex>0)
{
this.lblInfo.Text = this.ddl_BigCategory.SelectedValue;
}
}
加一个判断,如果你选择的是你自己添加的那个索引为0的值的话,会有错误
--------------------编程问答-------------------- 我按LS的试了下,还是不行,我断点输出SelectedIndex的值都为-1,所以this.lblInfo.Text为空,我想问为什么SelectedIndex的值都为-1?,我要怎么样才能取到我选中的值? --------------------编程问答-------------------- Request.Form["DropDownListID"] --------------------编程问答-------------------- 你可以在选择的查询出来的结果后面加上赋给哪个dropdownlist --------------------编程问答-------------------- 你也可以这样写获取dropdonwlist的值
string strSql = "SELECT [positionid], [positionname] FROM [positionnameid]";
conn.Open();
SqlCommand cmd = new SqlCommand(strSql, conn);
SqlDataReader dr = cmd.ExecuteReader();
ddlzhiwei.Items.Clear();
int i = 0;
while (dr.Read())
{
string ID = dr.GetString(0);
string Name = dr.GetString(1);
ddlzhiwei.Items.Add(Name.ToString());
ddlzhiwei.Items[i].Value = ID;
ddlzhiwei.Items[i].Text = Name;
i++;
}
--------------------编程问答-------------------- 数据绑定后加上
if(ddl_BigCategory.Items.Count > 0)
{
ddl_BigCategory.Items[0].Selected = true;
} --------------------编程问答-------------------- abandonship(eagles of wind)我试了,还是不行 --------------------编程问答-------------------- 没人能回答了吗? --------------------编程问答-------------------- DropDownList绑定后没有默认值,你指定ddl_BigCategory.Items[0].Selected = true就可以了
补充:.NET技术 , ASP.NET