我有两个复选框列表CandidateName和Payment,以及一个下拉列表。用户可以在复选框列表中进行多项选择,在下拉列表中进行一次选择。我将chechboxlists和dropdownlist的数据填充到嵌套在UpdatePanel里面的Panel中,其中gridview存在。当我点击Add Button打开Panel时,所有数据都正确填充。当我尝试通过" for循环"保存多个选定的复选框列表时在调试时,它不会进入循环内部以使用逗号累积已检查数据以保存在数据库中。相反,在chekcing" 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();
}
}
这是我的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"/>
<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-->
答案 0 :(得分:1)
由于这种情况,for
和cblCandidateName1.Items
的最后一项都不会执行您的cblPayment1.Items
个阻止:
for (int i = 0; i < cblCandidateName1.Items.Count-1; i++)
和
for (int j = 0; j < cblPayment1.Items.Count - 1; j++)
您需要将两个for
块更改为以下
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() + ",";
}
}
<强>更新强>
即使您查看了部分付款清单,似乎cblPayment1.Items[j].Selected
始终为假。可能的原因可能是您在cblPayment1
方法中填充Page_Load
项,但未在if (!this.IsPostBack)
块内填充
protected void Page_Load(object sender, EventArgs e)
{
cblPayment1.DataSource = .... ;
cblPayment1.DataBind();
}
当您点击保存按钮时,Page_Load
方法将在btnAddNew_Click
之前执行,因此cblPayment1
将会重新填充,并且当您启用时,所有项目都将被选中达到btnAddNew_Click
方法。您需要更改Page_Load
代码,如下所示
protected void Page_Load(object sender, EventArgs e)
{
if (!this.IsPostBack)
{
cblPayment1.DataSource = .... ;
cblPayment1.DataBind();
}
}
答案 1 :(得分:0)
首先尝试不以您所显示的方式在循环中使用字符串连接。这是一个坏习惯,并且在以后会导致性能问题,因为每个连接都会为字符串重新分配新内存,然后将其复制到该位置。
如何在此代码中使用一些lambdas? 它将大大简化:
strcbl1=string.Join(",", cblCandidateName1.Items.Where(x=>x.Selected));
strcbl2=string.Join(",", cblPayment1.Items.Where(x=>x.Selected));
答案 2 :(得分:0)
首先,尽量不要以问题中显示的方式在循环中使用字符串连接。这是一个坏习惯,并在以后导致性能问题,因为每个串联都会为字符串重新分配新的内存,然后将其复制到该位置。
如何在此代码中使用一些lambda?这样会大大简化:
strcbl1 = string.Join(",", cblCandidateName1.Items.Where(x=>x.Selected));
strcbl2 = string.Join(",", cblPayment1.Items.Where(x=>x.Selected));