ASPX下拉列表选中的项目

时间:2017-01-05 23:54:00

标签: c# asp.net

我有这段代码在DB的下拉列表中显示项目。

 string query = "SELECT ID, Firstname, Lastname FROM Crew";
            string constr = ConfigurationManager.ConnectionStrings["constr1"].ConnectionString;
            using (SqlConnection con = new SqlConnection(constr))
            {
                using (SqlCommand cmd = new SqlCommand(query))
                {
                    cmd.CommandType = CommandType.Text;
                    cmd.Connection = con;
                    con.Open();
                    using (SqlDataReader sdr = cmd.ExecuteReader())
                    {
                        while (sdr.Read())
                        {
                            ListItem item = new ListItem();
                            item.Text = sdr["Lastname"].ToString() + ' ' + sdr["Firstname"].ToString();
                            item.Value = sdr["ID"].ToString();
                            item.Selected = ""
                            ddlCrew.Items.Add(item);
                        }
                    }
                    con.Close();
                }
            }
            ddlCrew.Items.Insert(0, new ListItem("--Select Crew--", "0"));

如何根据值选择项目?

表船员: CrewID,名字,姓氏,IsActive

表报告: ReportID,船员

所以我需要从表Crew中选择Firstname + Lastname,具体取决于在表Report中列Crew中输入的ID。

编辑:

托尼建议我现在有这个代码:

            string query = "SELECT RepCrewID, Firstname, Lastname FROM tblRepCrew; SELECT ServiceTicketReportID, InspectedBy FROM tblServiceTicketReport WHERE ServiceTicketID = 123";
            string constr = ConfigurationManager.ConnectionStrings["constr1"].ConnectionString;
            using (SqlConnection con = new SqlConnection(constr))
            {
                using (SqlCommand cmd = new SqlCommand(query))
                {
                    cmd.CommandType = CommandType.Text;
                    cmd.Connection = con;
                    con.Open();
                    using (SqlDataReader sdr = cmd.ExecuteReader())
                    {
                        while (sdr.Read())
                        {

                            ListItem item = new ListItem();
                            item.Text = sdr["Lastname"].ToString() + ' ' + sdr["Firstname"].ToString();
                            item.Value = sdr["RepCrewID"].ToString();   

                      if ( sdr["InspectedBy"].ToString() == sdr["RepCrewID"].ToString() )
                            {                        
                                item.Selected = true;
                            }

                            ddlCrew.Items.Add(item);
                        }
                    }
                    con.Close();
                }
            }
            ddlCrew.Items.Insert(0, new ListItem("-- Select Crew --", "0"));

我唯一需要做的就是IF声明。据我所知,我必须检查InspectedBy列(表tblServiceTicketReport)中的ID是否与RepCrewID相同。如果它是相同的选择它,如果没有或它的黑色,能够选择新的机组成员。像这样:

             if ( sdr["InspectedBy"].ToString() == sdr["RepCrewID"].ToString() )
                            {                        
                                item.Selected = true;
                            }

已编辑2

到目前为止,我有:

string query = "SELECT ServiceTicketReportID, InspectedBy FROM tblServiceTicketReport WHERE ServiceTicketID = 21336; SELECT RepCrewID, Firstname, Lastname FROM tblRepCrew";
            string constr = ConfigurationManager.ConnectionStrings["constr"].ConnectionString;
            using (SqlConnection con = new SqlConnection(constr))
            {
                using (SqlCommand cmd = new SqlCommand(query))
                {
                    cmd.CommandType = CommandType.Text;
                    cmd.Connection = con;
                    con.Open();
                    using (SqlDataReader sdr = cmd.ExecuteReader())
                    {
                      while (sdr.Read())
                        {
                           something = sdr["InspectedBy"].ToString(); 
                        }
                            if (sdr.NextResult())
                                {
                                    while (sdr.Read())
                                    {
                                        ListItem item = new ListItem();
                                        item.Text = sdr["Lastname"].ToString() + ' ' + sdr["Firstname"].ToString();
                                        item.Value = sdr["RepCrewID"].ToString();   

                  if ( something == sdr["RepCrewID"].ToString())
                        {                        
                            item.Selected = true;
                        }

                        ddlCrew.Items.Add(item);

                            }
                        }
                    }
                    con.Close();
                }
            }
            ddlCrew.Items.Insert(0, new ListItem("-- Select Crew --", "0"));

但我在这里犯了这个错误:

>>> Error executing child request for ServiceTicketReportEdit.aspx.
>>> ServiceTicketReportEdit.aspx.cs(58): error CS0103: The name 'something' does not exist in the current context

表:

tblServiceTicketReport:ServiceTicketReportID,ServiceTicketID,InspectedBy

tblRepCrew:RepCrewID,名字,姓氏

2 个答案:

答案 0 :(得分:1)

dropdownlist.ClearSelection(); //for clearing previous selection
dropdownlist.Items.FindByValue(value).Selected = true; //select item by value

答案 1 :(得分:1)

您可以检查while循环内的条件并选择默认值

        string query = "SELECT ServiceTicketReportID, InspectedBy FROM tblServiceTicketReport WHERE ServiceTicketID = 123; SELECT RepCrewID, Firstname, Lastname FROM tblRepCrew";
        string constr = ConfigurationManager.ConnectionStrings["constr1"].ConnectionString;
        string something = null; 
        using (SqlConnection con = new SqlConnection(constr))
        {
            using (SqlCommand cmd = new SqlCommand(query))
            {
                cmd.CommandType = CommandType.Text;
                cmd.Connection = con;
                con.Open();
                using (SqlDataReader sdr = cmd.ExecuteReader())
                {
                    while (sdr.Read())
                    {
                       something = sdr["InspectedBy"].ToString(); 
                    }
                    if (sdr.NextResult())
                    {
                    while (sdr.Read())
                    {

                        ListItem item = new ListItem();
                        item.Text = sdr["Lastname"].ToString() + ' ' + sdr["Firstname"].ToString();
                        item.Value = sdr["RepCrewID"].ToString();   

                  if ( something == sdr["RepCrewID"].ToString())
                        {                        
                            item.Selected = true;
                        }

                        ddlCrew.Items.Add(item);
                    }
                  }
                }
                //using auto close the connection
                //con.Close();

            }
        }
        ddlCrew.Items