[英]CheckboxList on deselect remove the item from Bulleted list
Hi I have this piece of code where I map the selected values of my check boxes to a Bulleted list so far it work ok when I select the value - it maps to bulleted list accordingly. 嗨,我有这段代码,我在其中将复选框的选定值映射到项目符号列表,到目前为止,当我选择该值时,它可以正常工作-相应地,它映射到项目符号列表。 However, I am struggling a bit when users deselects the value.
但是,当用户取消选择该值时,我有点挣扎。 I would like it to update the bulledted list accordingly.
我希望它相应地更新项目符号列表。 Any help?
有什么帮助吗?
Here is the code: 这是代码:
public List<string> elements = new List<string>();
protected void checkList_SelectedIndexChanged(object sender, EventArgs e)
{ foreach(ListItem item in checkList.Items)
{
if (item.Selected)
{
elements.Add(item.Text);
}
else {//Should this else triger the deletion of the elements in the list?
}
}
foreach (object o in elements)
{ int exists = 0;
for (int i = 0; i < BullList.Items.Count; i++)
{
if(BullList.Items[i].ToString() == o.ToString())
{
exists++;
}
}
if(exists == 0)
{
BullList.Items.Add(o.ToString());
}}}
protected void checkList_DataBound(object sender, EventArgs e)
{
for (int i = 0; i < BullList.Items.Count; i++)
{
foreach (ListItem item in checkList.Items)
{
if (BullList.Items[i].ToString() == item.ToString())
{
item.Selected = true;
}
else
{
}}}}
The DOM elements: DOM元素:
<asp:DropDownList AutoPostBack="true" ID="DropDownJobs" runat="server" Width="390px" Height="42px" Font-Names="Roboto" Font-Size="25px" DataSourceID="jobsDrop" DataTextField="MainJob" DataValueField="IDKey"></asp:DropDownList>
<asp:CheckBoxList OnDataBound="checkList_DataBound" AutoPostBack="true" ID="checkList" runat="server" OnSelectedIndexChanged="checkList_SelectedIndexChanged" DataSourceID="SortedListJobs" DataTextField="SubJobFamily" DataValueField="SubJobFamily" Width="406px">
<asp:ListItem>
</asp:ListItem>
</asp:CheckBoxList>
<h6 class="List">These jobs have been selected:</h6>
<asp:BulletedList runat="server" ID="BullList" CssClass="bullList"></asp:BulletedList/>
Like you are adding item to BulletedList
create a list
of deselect item
. 就像您将项目添加到
BulletedList
创建一个deselect item
list
。 And then remove it from BulletedList
. 然后将其从
BulletedList
删除。 Like below 像下面
public List<string> elements = new List<string>();
public List<string> deselect = new List<string>();
protected void checkList_SelectedIndexChanged(object sender, EventArgs e)
{ foreach(ListItem item in checkList.Items)
{
if (item.Selected)
{
elements.Add(item.Text);
}
else
{
deselect.Add(item.Text);
}
}
foreach (object o in elements)
{ int exists = 0;
for (int i = 0; i < BullList.Items.Count; i++)
{
if(BullList.Items[i].ToString() == o.ToString())
{
exists++;
}
}
if(exists == 0)
{
BullList.Items.Add(o.ToString());
}
}
foreach (object o in deselect)
{
for (int i = 0; i < BullList.Items.Count; i++)
{
BullList.Items.Remove(o.ToString());
}
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.