我在一个Dropdownlist中列出了员工姓名。通过选择任何员工,我将在数据库的gridview中显示员工详细信息。C#ASP.Net中的DropDownList清除选择
默认情况下,dropdownlist将第一项作为选定项目。因此当我选择另一项时,它仅返回第一个索引。 我的代码:
protected void FilterBtn_Click(object sender, EventArgs e)
{
if (EmployeeList.SelectedIndex > -1)
{
sInitQuery = sInitQuery + " WHERE (EmployeeName ='" + EmployeeList.SelectedItem.ToString() + "')";
}
if (GlobalCS.OpenConnection() == true)
{
GridView1.DataSource = null;
GridView1.DataBind();
MySqlCommand cmd = new MySqlCommand(sInitQuery, GlobalCS.objMyCon);
MySqlDataReader reader = cmd.ExecuteReader();
GridView1.DataSource = reader;
GridView1.DataBind();
reader.Close();
}
GlobalCS.CloseConnection();
EmployeeList.ClearSelection();
}
你能不能展示你的页面载入,问题是在那里我想,你是否在下拉菜单中调用了'if(!Page.IsPostBack)''? –
protected void Page_Load(object sender,EventArgs e) DisplayDatainGrid(); // GridView中的所有数据 AddDataDropList(); //在Dropdownlist中加载数据 } – Anu
设计建议:您不需要按钮。您可以直接在DropDownList.SelectedIndexChanged()事件中编写此代码。 –