Asp.net代码:
Select Employee : </b>
<asp:DropDownList ID="DropDownListSelectEmployee" runat="server" AutoPostBack="true"
OnSelectedIndexChanged="DropDownListSelectEmployee_SelectedIndexChanged"
OnTextChanged="DropDownListSelectEmployee_TextChanged" >
</asp:DropDownList>
代码背后:
public void DropDownListSelectEmployee_Fill()
{
DropDownListSelectEmployee.Items.Clear();
string q = "select username from nworksuser where _type='Employee' or _type='Admin_Employee';";
MySqlCommand cmd = new MySqlCommand(q, conn);
conn.Open();
string user = "";
MySqlDataReader rdr = cmd.ExecuteReader();
while (rdr.Read())
{
user = rdr.GetString("username");
DropDownListSelectEmployee.Items.Add(user);
}
conn.Close();
}
protected void DropDownListSelectEmployee_TextChanged(object sender, EventArgs e)
{
string q = "select uid,fname,mname,lname,date_format(hire_date,'%Y-%m-%d'),desg,date_format(dob,'%Y-%m-%d'),AddressLine1,AddressLine2,state,city,pincode,country,MobileNo,UserActive,username,date_format(annivarsary,'%Y-%m-%d'),email from nWorksUser where Username='" + DropDownListSelectEmployee.Text + "'";
MySqlCommand cmd = new MySqlCommand(q, conn);
conn.Open();
MySqlDataAdapter dataAdapter = new MySqlDataAdapter(q, conn);
DataTable dt = new DataTable();
dataAdapter.Fill(dt);
foreach (DataRow dr in dt.Rows)
{
txtFirstName.Text = dr[1].ToString();
txtMiddleName.Text = dr[2].ToString();
txtLastName.Text = dr[3].ToString();
txtMobile.Text = dr[13].ToString();
txtUsername.Text = dr[15].ToString();
txtState.Text = dr[9].ToString();
txtPincode.Text = dr[11].ToString();
txtEmail.Text = dr[17].ToString();
txtDob.Text = dr[6].ToString();
txtDesignation.Text = dr[5].ToString();
txtDateofHire.Text = dr[4].ToString();
txtDateofAnnivarsary.Text = dr[16].ToString();
txtCountry.Text = dr[12].ToString();
txtCity.Text = dr[10].ToString();
txtAddressLine2.Text = dr[7].ToString();
txtAddressLine1.Text = dr[8].ToString();
if (dr[14].ToString().Length == 4)
{
RdoGender.SelectedIndex = 1;
}
else
{
RdoGender.SelectedIndex = 2;
}
conn.Close();
}
}
protected void DropDownListSelectEmployee_SelectedIndexChanged(object sender, EventArgs e)
{
}
下拉列表包含来自数据库的用户名。现在我希望如此,当你从下拉列表中选择任何用户时,它应该在表单和工作中显示他的详细信息。但问题是,在选择任何一个用户之后,该名称会在下拉列表中显示,并将其重置为下拉列表的第一个名称,任何显示其相关的详细信息而不是选定的。它除了名字下拉列表外不接受任何其他名称。我不知道问题出在哪里。请帮帮我。
答案 0 :(得分:0)
您需要使用DropDownListSelectEmployee_Fill
将代码包装在if (!Page.IsPostBack)
中。在帖子后面,下拉内容将重新填充,导致其状态重置,这就是为什么您会看到&#34;意外行为&#34;。
检查回发后,您可以确定下拉列表不会因选择更改而受到影响:
public void DropDownListSelectEmployee_Fill()
{
if (!Page.IsPostBack)
{
DropDownListSelectEmployee.Items.Clear();
string q = "select username from nworksuser where _type='Employee' or _type='Admin_Employee';";
MySqlCommand cmd = new MySqlCommand(q, conn);
conn.Open();
string user = "";
MySqlDataReader rdr = cmd.ExecuteReader();
while (rdr.Read())
{
user = rdr.GetString("username");
DropDownListSelectEmployee.Items.Add(user);
}
conn.Close();
}
}