[英]Disable and checked values of `CheckBoxList` memorized in database in c#
I have added in an form in aspx page this CheckBoxList
: 我在aspx页面的表单中添加了此
CheckBoxList
:
<asp:CheckBoxList ID="Fruits" runat="server">
<asp:ListItem Text="Mango" Value="1" />
<asp:ListItem Text="Apple" Value="2" />
<asp:ListItem Text="Banana" Value="3" />
<asp:ListItem Text="Guava" Value="4" />
<asp:ListItem Text="Orange" Value="5" />
</asp:CheckBoxList>
In this CheckBoxList
it's possible select multiple values, eg : 在此
CheckBoxList
,可以选择多个值,例如:
And this values are memorized in field of database table: 并将此值存储在数据库表的字段中:
for (int i = 0; i < Fruits.Items.Count; i++)
{
if (Fruits.Items[i].Selected == true)
{
FruitsUpdate += Fruits.Items[i].Text.ToString() + ";";
}
}
Value memorized in database : 1;4;5;
存储在数据库中的值:
1;4;5;
Now in aspx form page I need disable and checked the values of CheckBoxList
memorized in database, but I have tried this code without success because all ListItem
are disabled but not checked. 现在,在aspx表单页面中,我需要禁用并检查存储在数据库中的
CheckBoxList
的值,但是由于所有ListItem
均被禁用但未选中,因此我尝试了此代码,但未成功。
FruitsDB = dr["Fruits"].ToString();
if (FruitsDB.ToString() != "")
{
Fruits.Text = FruitsDB.ToString();
Fruits.Enabled = false;
}
else
{
Fruits.Enabled = true;
}
I need disable and checked in the CheckBoxList the items with value 1 and 4 and 5. 我需要禁用并在CheckBoxList中检查值为1、4和5的项目。
Please help me, thank you in advance. 请帮助我,谢谢你。
You didn't set the Selected
property of the list item where you set the Enabled
property. 您没有在设置
Enabled
属性的位置设置列表项的Selected
属性。
Here is the code. 这是代码。 You will iterate over CheckBoxList.
您将遍历CheckBoxList。 if current checkbox's value is in FruitsDB then it will be selected and disabled.
如果当前复选框的值在FruitsDB中,则将其选中并禁用。
var memory = FruitsDB.ToString();
foreach (ListItem item in Fruits.Items)
{
if (memory.Contains(item.Value))
{
item.Enabled = false;
item.Selected = true;
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.