简体   繁体   中英

drop down list value in asp.net (by Visual Studio)

Drop Down List Problem

I have a fairly simple question.

I have a drop down list that is populated by a table in my sql database. Now I am retrieving values into a drop down list but these values go to the last row in the drop down list (value is machine1). Now I want to get value on top by user select Employee name.

在此处输入图片说明

Code:

string select = "select * from dally_report where Emp_name='" + txt_search.SelectedItem.ToString()+ "'";
   MySqlCommand cmd1 = new MySqlCommand(select, DBConnection.connection());
   MySqlDataAdapter sp = new MySqlDataAdapter();
   MySqlDataReader dr = cmd1.ExecuteReader();

        if (dr.Read()) 
        {
            txt_date.Text = dr[2].ToString();
            txt_empname.Text = dr[1].ToString();
            txt_jobno.Text = dr[3].ToString();
            txt_machine1.Items.Add (dr[4].ToString()); // This is drop down list
        }

代替使用Items.Add尝试使用Items.Insert索引为0

Try this code

<asp:DropDownList ID="drp_district" runat="server" EnableViewState="true" OnSelectedIndexChanged="drp_district_SelectedIndexChanged" AutoPostBack="true" CssClass="form-control" autocomplete="off"></asp:DropDownList>

   protected void drp_district_SelectedIndexChanged(object sender, EventArgs e)
        {
            string value = drp_district.SelectedValue;

            _objdll.TableName = "tbl_assemblydetails";
            _objdll.Condition = "district_id";
            _objdll.Value = value;

            DataSet ds_assembly = _objdal.Fn_GEN_SelectAll_1Condition(strcommoncon, _objdll);
            if (ds_assembly.Tables[0].Rows.Count > 0)
            {
                drp_assembly.DataTextField = "assembly_english".ToUpper();
                drp_assembly.DataValueField = "assembly_id";
                drp_assembly.DataSource = ds_assembly;
                drp_assembly.DataBind();
            }
            else
            {
                drp_assembly.DataSource = null;
                drp_assembly.DataBind();
                drp_assembly.Items.Insert(0, new ListItem("Select Assembly", "0"));
            }
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM