检索复选框值并标记为选中



>我正在尝试从数据库中检索选项到复选框,如果它具有相同的产品ID,则必须默认选中

这是将代码插入到数据库

for (int i = 0; i < ddlcaroptions.Items.Count; i++)
{
   if (ddlcaroptions.Items[i].Selected == true)
{
Int64 PCarOptionID = Convert.ToInt64(ddlcaroptions.Items[i].Value);
SqlCommand cmd2 = new SqlCommand("insert into tblCarOptionQuant values('" + PID + "','" + PCarOptionID + "')", con);
cmd2.ExecuteNonQuery();
}
}

现在工作正常,我想编辑此复选框并更新数据库现在,我绑定了另一个具有名称和值的表的复选框


SqlCommand cmd = new SqlCommand("select * from tblCarOption", con);
                SqlDataAdapter sda = new SqlDataAdapter(cmd);
                DataTable dt = new DataTable();
                sda.Fill(dt);
                if (dt.Rows.Count != 0)
                {
                    ddlcaroptions.DataSource = dt;
                    ddlcaroptions.DataTextField = "CarOptionNameAR";
                    ddlcaroptions.DataValueField = "CarOptionID";
                    ddlcaroptions.DataBind();
                }

现在我有所有选项,但我希望所选值是用户之前已经检查并保存在 tblCarOptionQuant 表中的值,

我试图通过此命令获取值

using (SqlCommand getselectedop = new SqlCommand("select PCarOptionID from tblCarOptionQuant where PID = " + PID + "", con))

没关系,但现在我该怎么做才能从此命令结果中设置选定的值??

您可以使用

FindByValue获取特定项目并将其设置为selected,请参阅下面的代码

string value = "SomeValue";
               var item = ddlcompany.Items.FindByValue(value);
               if (item != null)
                   item.Selected = true;

问题是当您检索数据并转换为字符串时,您只会得到第一行,所以我使用 StringBuilder 构建 1 个字符串,其中包含表 :) 中的所有行!!注意力!!在使用此代码之前,请确保将复选框与用户选中或未选中的所有值绑定,然后使用此代码获取之前(由用户(选择的选项

                using (SqlCommand getselectedop = new SqlCommand("RetrieveCarOptions", con))
            {
                getselectedop.CommandType = CommandType.StoredProcedure;
                getselectedop.Parameters.AddWithValue("@PID", Convert.ToInt64(Request.QueryString["PID"]));
                con.Open();
                using (SqlDataReader reader = getselectedop.ExecuteReader())
                {
                    StringBuilder sb = new StringBuilder();
                    if (reader.HasRows)
                    {
                        if (sb.Length > 0) sb.Append("___");
                        while (reader.Read())
                        {
                            for (int i = 0; i < reader.FieldCount; i++)
                                if (reader.GetValue(i) != DBNull.Value)
                                    sb.AppendFormat("{0}-", Convert.ToString(reader.GetValue(i)));
                            SelectCheckBoxList(reader["PCarOptionID"].ToString(), ddlcaroptions);
                        }
                    }
                }
            }

现在拆分并检查它是否具有相同的值以标记为选中

        private void SelectCheckBoxList(string valueToSelect, CheckBoxList ddlcaroptions)
    {
        string[] aray = valueToSelect.Split(',');
        foreach (string listItem2 in aray)
        {
            ListItem listItem = ddlcaroptions.Items.FindByValue(valueToSelect);
            listItem.Selected = true;                
        }
    }

我希望这个答案是明确的,并帮助其他人:)

最新更新