如何将多个选中的复选框列表保存到数据库asp.NETC#

如何将多个选中的复选框列表保存到数据库asp.NETC#,c#,sql,asp.net,checkboxlist,C#,Sql,Asp.net,Checkboxlist,我有两个清单,一个是姓名和付款清单,另一个是下拉列表。用户可以在复选框列表中进行多项选择,在下拉列表中进行一项选择。我正在将chechboxlists和dropdownlist的数据填充到存在gridview的UpdatePanel中嵌套的面板中。。当我点击Add按钮打开面板时,所有数据都被正确填充。当我试图在调试时通过“for loop”保存多个选中的checkedboxlists时,它不会在循环内部使用逗号累积选中的数据以保存在数据库中。相反,在选中“if(cblPayment1.Items

我有两个清单,一个是姓名和付款清单,另一个是下拉列表。用户可以在复选框列表中进行多项选择,在下拉列表中进行一项选择。我正在将chechboxlists和dropdownlist的数据填充到存在gridview的UpdatePanel中嵌套的面板中。。当我点击Add按钮打开面板时,所有数据都被正确填充。当我试图在调试时通过“for loop”保存多个选中的checkedboxlists时,它不会在循环内部使用逗号累积选中的数据以保存在数据库中。相反,在选中“if(cblPayment1.Items[j].Selected==true)”之后,它会跳转到另一个查询语句。请帮我提建议

 protected void btnAddNew_Click(object sender, EventArgs e)
{
    try
    {

        string strcbl1 = string.Empty;
        string strcbl2 = string.Empty;
        compCon = new SqlConnection(strConnectionString);
        compCon.Open();


        for (int i = 0; i < cblCandidateName1.Items.Count-1; i++)
        {
            if (cblCandidateName1.Items[i].Selected ==true)
            {
                strcbl1 = strcbl1 + cblCandidateName1.Items[i].Value.ToString() + ",";
            }
        }

        for (int j = 0; j < cblPayment1.Items.Count - 1; j++)
        {
            if (cblPayment1.Items[j].Selected == true)
            {
                strcbl2 += cblPayment1.Items[j].Value.ToString() + ",";
            }
        }


       string InsertSchedule = "INSERT INTO ScheduleEmail(CANDIDATE_ID, PAYMENT_ID,     TEMPLATE_ID)VALUES(@strCID, @strPID, @strCourseID)";
        SqlCommand InsertScheduleCommand = new SqlCommand(InsertSchedule, compCon);
        InsertScheduleCommand.Connection = compCon;


        InsertScheduleCommand.Parameters.AddWithValue(@"strCID", strcbl1);
        InsertScheduleCommand.Parameters.AddWithValue(@"strPID", strcbl2);
        InsertScheduleCommand.Parameters.AddWithValue(@"strCourseID", strCourseID);

        InsertScheduleCommand.ExecuteNonQuery();
        compCon.Close();
    }
    catch (Exception ex)
    {
        ShowPopUpMsg("Data is failed to save in table. Please contact your system administrator \n" + ex.ToString());
    }
    finally
    { 
    DispalyGridViewScheduleEmail();

    compCon.Close();
    }

}
protectedvoid btnAddNew_单击(对象发送方,事件参数e)
{
尝试
{
string strcbl1=string.Empty;
string strcbl2=string.Empty;
compCon=新的SqlConnection(strConnectionString);
compCon.Open();
对于(int i=0;i
这是我的HTML代码

<!--Panel to add new record--><asp:Panel ID="panelAddNew" runat="server" 
style="display:none; background-color:gray;"        ForeColor="Black" Width="500" Height="550">
<asp:Panel ID="panelAddNewTitle" runat="server" 
 style="cursor:move;font-family:Tahoma;                       
 padding:2px;" HorizontalAlign="Center" BackColor="Blue" ForeColor="White" Height="25">
<b>Add New</b></asp:Panel>
 <table width="100%" style="padding:5px">
            <tr>
            <td colspan="3">
                <asp:Label ID="lblStatus1" runat="server"></asp:Label>
            </td>
            </tr>
            <tr>
                <td><b>Select Candidate Name(s)</b></td>
                <td><b>:</b></td>
                <td><asp:CheckBoxList ID="cblCandidateName1" runat="server" RepeatColumns="3" RepeatDirection="Horizontal" RepeatLayout="table">
                </asp:CheckBoxList>
                </td>
            </tr>
            <tr>
                <td><b>Select Payments</b></td>
                <td><b>:</b></td>
                <td>
                   <asp:CheckBoxList ID="cblPayment1" runat="server" RepeatDirection="Horizontal" RepeatLayout="Table"></asp:CheckBoxList>
                </td>
            </tr>


            <tr>
                <td><b>Course Name</b></td>
                <td><b>:</b></td>
                <td> <asp:DropDownList ID="ddlCourseName" runat="server">      </asp:DropDownList>            


                </td>
            </tr>
        </table>
        <br />
            <div align="center">
            <asp:Button ID="btnAddNew2" runat="server" Width="70" Text="Add" OnClick="btnAddNew_Click"  ValidationGroup="add"/>
            &nbsp;

            <asp:Button ID="btnCancel1" runat="server" Width="70" Text="Cancel" CausesValidation="false" OnClick="Cancel_Click" ValidationGroup="add"/>
        </div>
    </asp:Panel>   
        <!--End of Panel to add new record-->

新增
选择候选名称
:
选择付款
:
课程名称
:


由于以下情况,您的
for
块的
将不会在
cblCandidateName1.Items
cblPayment1.Items
的最后一项执行:

for (int i = 0; i < cblCandidateName1.Items.Count-1; i++)
更新

看起来,
cblPayment1.Items[j]。选中的
始终为false,即使您选中了一些付款复选框列表。可能的原因可能是您在
Page\u Load
方法中填充
cblPayment1
项,但不在
if(!this.IsPostBack)
块中

protected void Page_Load(object sender, EventArgs e)
{
    cblPayment1.DataSource = .... ;
    cblPayment1.DataBind();
} 
当您单击“保存”按钮时,在单击“新建”按钮之前,将首先执行“加载页面”方法,因此当您到达“新建页面”方法时,将重新填充“新建页面”,并且不会选择其所有项目。您需要更改
页面加载
代码,如下所示

for (int i = 0; i < cblCandidateName1.Items.Count; i++)
{
    if (cblCandidateName1.Items[i].Selected ==true)
    {
        strcbl1 = strcbl1 + cblCandidateName1.Items[i].Value.ToString() + ",";
    }
}

for (int j = 0; j < cblPayment1.Items.Count; j++)
{
    if (cblPayment1.Items[j].Selected == true)
    {
        strcbl2 += cblPayment1.Items[j].Value.ToString() + ",";
    }
}
protected void Page_Load(object sender, EventArgs e)
{
    if (!this.IsPostBack)
    {
        cblPayment1.DataSource = .... ;
        cblPayment1.DataBind();
    }
} 

首先,不要像前面提到的那样在循环中使用字符串连接。这是一个坏习惯,会在以后导致性能问题,因为每次连接都会为字符串重新分配新内存,然后将其复制到那个位置

在代码中使用lambda怎么样? 它将被大大简化如下:

strcbl1=string.Join(",", cblCandidateName1.Items.Where(x=>x.Selected));
strcbl2=string.Join(",", cblPayment1.Items.Where(x=>x.Selected));
strcbl1 = string.Join(",", cblCandidateName1.Items.Where(x=>x.Selected));

strcbl2 = string.Join(",", cblPayment1.Items.Where(x=>x.Selected));

首先,尽量不要像您在问题中所展示的那样在循环中使用字符串连接。这是一个坏习惯,会在以后导致性能问题,因为每次串联都会为字符串重新分配新内存,然后将其复制到那个位置

在代码中使用lambda怎么样?它将被大大简化如下:

strcbl1=string.Join(",", cblCandidateName1.Items.Where(x=>x.Selected));
strcbl2=string.Join(",", cblPayment1.Items.Where(x=>x.Selected));
strcbl1 = string.Join(",", cblCandidateName1.Items.Where(x=>x.Selected));

strcbl2 = string.Join(",", cblPayment1.Items.Where(x=>x.Selected));

我删除了负1并进行了调试。它仍然没有进入“如果”语句。谢谢您的帮助。您是否检查了
cblPayment1.Items[j]。Selected
是否为真?是否为真。我选择了付款清单中的2个,谢谢。这是有道理的。我没有把代码放在页面加载中。这就是它根本不循环的原因。它现在正在工作。再次感谢。