在asp.net C中单击一次即可插入多行表单复选框列表选定值#

在asp.net C中单击一次即可插入多行表单复选框列表选定值#,asp.net,selecteditem,checkboxlist,Asp.net,Selecteditem,Checkboxlist,我是C#ASP.NET的新手。我在复选框列表中遇到问题,无法在数据库中添加多行的选定项 我有一个页面,我在数据库中的提交按钮上插入产品信息。在页面中,我从数据库绑定复选框列表 我的产品代码将在我签入复选框时自动生成。若我从复选框列表中选择了2个复选框,那个么数据库中只会插入一个复选框列表值。未在数据库中插入其他选中的复选框值或信息 在我的Sql表中,我插入产品名称、产品代码(将自动生成)、产品标题等 所以我希望在一次提交点击选中复选框列表时,在数据库中添加多行 我的代码是 foreach (Li

我是C#ASP.NET的新手。我在复选框列表中遇到问题,无法在数据库中添加多行的选定项

我有一个页面,我在数据库中的提交按钮上插入产品信息。在页面中,我从数据库绑定复选框列表

我的产品代码将在我签入复选框时自动生成。若我从复选框列表中选择了2个复选框,那个么数据库中只会插入一个复选框列表值。未在数据库中插入其他选中的复选框值或信息

在我的Sql表中,我插入产品名称、产品代码(将自动生成)、产品标题等

所以我希望在一次提交点击选中复选框列表时,在数据库中添加多行

我的代码是

foreach (ListItem listitem in cblsubcat.Items)
{
  if (listitem.Selected == true)
  {
    product myproduct;
    myproduct = new product();
    myproduct.ProdName = txtproductname.Text.ToString().Trim();
    GetCategoryId();
    int Subcat_id = int.Parse(cblsubcat.SelectedValue.ToString());
    int Cat_id = int.Parse(cateid.ToString());
    gen_prod_code(Cat_id, Subcat_id);
    myproduct.ProdCode = prodcode;
    myproduct.ProdTitle = txtproductname.Text.ToString().Trim();
    myproduct.CatId = cateid;
    myproduct.SubCatID = int.Parse(cblsubcat.SelectedValue.ToString());
    ProductManager.InsertProductdata(myproduct);
  }
这是我在Dataaccesslayer中的InsertProductdata代码

public static int InsertProductdata(product myproduct)
    {
        int result = 0;
        using (SqlConnection myConnection = new SqlConnection(AppConfiguration.ConnectionString))
        {
            SqlCommand myCommand = new SqlCommand("spInsertProductItem", myConnection);
            myCommand.CommandType = CommandType.StoredProcedure;
            myCommand.Parameters.AddWithValue("@Prod_code", myproduct.ProdCode);
            myCommand.Parameters.AddWithValue("@Prod_name", myproduct.ProdName);                
            myCommand.Parameters.AddWithValue("@Prod_title", myproduct.ProdTitle);
            myCommand.Parameters.AddWithValue("@Prod_sdesc", myproduct.ProdSdesc);
            myCommand.Parameters.AddWithValue("@Prod_ldesc", myproduct.ProdLdesc);
            myCommand.Parameters.AddWithValue("@Prod_features", myproduct.ProdFeatures);
            myCommand.Parameters.AddWithValue("@Prod_colour", myproduct.ProdColour);
            myCommand.Parameters.AddWithValue("@Prod_size", myproduct.ProdSize);
            myCommand.Parameters.AddWithValue("@Prod_height_FI", myproduct.ProdHeightFi);
            myCommand.Parameters.AddWithValue("@Prod_height_CM", myproduct.ProdHeightCm);
            myCommand.Parameters.AddWithValue("@Prod_totqty", myproduct.ProdTotqty);
            myCommand.Parameters.AddWithValue("@Prod_available_size", myproduct.ProdAvailablesize);
            myCommand.Parameters.AddWithValue("@Prod_available_qty", myproduct.ProdAvailableqty);                
            myCommand.Parameters.AddWithValue("@Prod_price", myproduct.ProdPrice);
            myCommand.Parameters.AddWithValue("@Prod_offerprice", myproduct.ProdOfferprice);
            myCommand.Parameters.AddWithValue("@Prod_img_M", myproduct.ProdImgM);
            myCommand.Parameters.AddWithValue("@Prod_img_D", myproduct.ProdImgD);
            myCommand.Parameters.AddWithValue("@Prod_img_S", myproduct.ProdImgS);
            myCommand.Parameters.AddWithValue("@Prod_img_L", myproduct.ProdImgL);
            myCommand.Parameters.AddWithValue("@Prod_img_XL", myproduct.ProdImgXL);
            myCommand.Parameters.AddWithValue("@Prod_img_F", myproduct.ProdImgF);
            myCommand.Parameters.AddWithValue("@Prod_img_B", myproduct.ProdImgB);
            myCommand.Parameters.AddWithValue("@Prod_visibility", myproduct.ProdVisibility);
            myCommand.Parameters.AddWithValue("@Prod_is_del", myproduct.Prodisdel);
            myCommand.Parameters.AddWithValue("@Prod_createddate", myproduct.ProdCreateddate);
            myCommand.Parameters.AddWithValue("@Prod_modifieddate", myproduct.ProdModifieddate);
            myCommand.Parameters.AddWithValue("@Prod_keyword", myproduct.ProdKeyword);
            myCommand.Parameters.AddWithValue("@Prod_alttag", myproduct.ProdAlttag);
            myCommand.Parameters.AddWithValue("@Cat_id", myproduct.CatId);
            myCommand.Parameters.AddWithValue("@Subcat_id", myproduct.SubCatID);
            myCommand.Parameters.AddWithValue("@is_Featured", myproduct.IsFeatured);
            myCommand.Parameters.AddWithValue("@is_Recent", myproduct.IsRecent);
            myCommand.Parameters.AddWithValue("@is_Popular", myproduct.IsPopular);
            myCommand.Parameters.AddWithValue("@title_tag", myproduct.TitleTag);
            myCommand.Parameters.AddWithValue("@meta_description", myproduct.MetaDescription);

            DbParameter returnValue;
            returnValue = myCommand.CreateParameter();
            returnValue.Direction = ParameterDirection.ReturnValue;
            myCommand.Parameters.Add(returnValue);

            myConnection.Open();
            myCommand.ExecuteNonQuery();
            result = Convert.ToInt32(returnValue.Value);
            myConnection.Close();
        }
        return result;
    }

分享一些您尝试过的代码片段。您能发布InsertProductdata的代码吗?先生,我正在使用sql存储过程与Asp.net 3一起使用。确实,cblsubcat.Items值已填充吗?尝试在循环中放入一些调试代码。例如:if(listitem.Selected==true){Response.Write(DateTime.Now.ToString())+“@user1986324,为什么不通过在相关代码中设置断点来尝试调试?当您选择了多个项目时,代码应该被调用多次。另一方面,从用户的角度来看,在单个DB事务中插入所有产品以进行原子操作更有意义。