下面是我的CheckBoxList设计代码。
<asp:CheckBoxList ID="chklst_assistance_seeking" runat="server"
AutoPostBack="True" OnSelectedIndexChanged="chklst_assistance_seeking_SelectedIndexChanged" RepeatDirection="Vertical">
<asp:ListItem Text="Mentoring" Value="Mentoring"></asp:ListItem>
<asp:ListItem Text="Seed Funding" Value="Seed Funding"></asp:ListItem>
<asp:ListItem Text="Assistance in Market Access" Value="Assistance in Market Access"></asp:ListItem>
<asp:ListItem Text="Help with certification" Value="Help with certification"> </asp:ListItem>
<asp:ListItem Text="Other" Value="Other"></asp:ListItem>
</asp:CheckBoxList>
下面是我的复选框列表选择索引更改代码。
protected void chklst_assistance_seeking_SelectedIndexChanged(object sender, EventArgs e)
{
string assistance_seeking = "";
Collection<string> items = new Collection<string>();
for (int index = 0; index < chklst_assistance_seeking.Items.Count; index++)
{
if (chklst_assistance_seeking.Items[index].Selected)
{
items.Add(chklst_assistance_seeking.Items[index].ToString());
}
}
assistance_seeking = String.Join(",", items.ToArray());
string[] selectedvalues = assistance_seeking.Split(',');
for (int i = 0; i < selectedvalues.Length; i++)
{
selectedvalues[i] = selectedvalues[i].Trim();
if (selectedvalues[i].ToString() == "Help with certification")
{
pnl_assistance_seeking_help.Visible = true;
}
else if (selectedvalues[i].ToString() == "Other")
{
pnl_assistance_seeking_Other.Visible = true;
}
else if (selectedvalues[i].ToString() == "Mentoring")
{
pnl_assistance_seeking_Mentoring.Visible = true;
}
else
{
pnl_assistance_seeking_help.Visible = false;
pnl_assistance_seeking_Other.Visible = false;
pnl_assistance_seeking_Mentoring.Visible = false;
}
}
}
当我选择checkboxlist项目时,面板在逐个选择后可以正确显示,但是当我逐个取消选择checkboxlist项目时,Panel会逐个显示为false。所有最后一次取消选择都显示为假。
答案 0 :(得分:0)
在循环内面板的可见性被重置为false,因此每次迭代都可能发生这种情况,这似乎是错误的。如果任何if
语句为真,您也不会重置其他面板的状态。
我建议采用以下逻辑:
这样,您应该始终具有与复选框的当前状态相对应的面板可见性。在代码中看起来像这样:
pnl_assistance_seeking_IIGP_help.Visible = false;
pnl_assistance_seeking_IIGP_Other.Visible = false;
pnl_assistance_seeking_IIGP_Mentoring.Visible = false;
string[] selectedvalues = assistance_seeking_IIGP.Split(',');
for (int i = 0; i < selectedvalues.Length; i++)
{
selectedvalues[i] = selectedvalues[i].Trim();
if (selectedvalues[i].ToString() == "Help with certification")
{
pnl_assistance_seeking_IIGP_help.Visible = true;
}
else if (selectedvalues[i].ToString() == "Other")
{
pnl_assistance_seeking_IIGP_Other.Visible = true;
}
else if (selectedvalues[i].ToString() == "Mentoring")
{
pnl_assistance_seeking_IIGP_Mentoring.Visible = true;
}
}