在 c# 中禁用和检查存储在数据库中的 `CheckBoxList` 的值
Disable and checked values of `CheckBoxList` memorized in database in c#
我已经在 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>
在此 CheckBoxList
中可以 select 多个值,例如:
并且这个值被存储在数据库的字段中table:
for (int i = 0; i < Fruits.Items.Count; i++)
{
if (Fruits.Items[i].Selected == true)
{
FruitsUpdate += Fruits.Items[i].Text.ToString() + ";";
}
}
数据库中存储的值:1;4;5;
现在在 aspx 表单页面中,我需要禁用并检查存储在数据库中的 CheckBoxList
的值,但我尝试此代码但没有成功,因为所有 ListItem
都被禁用但未检查。
FruitsDB = dr["Fruits"].ToString();
if (FruitsDB.ToString() != "")
{
Fruits.Text = FruitsDB.ToString();
Fruits.Enabled = false;
}
else
{
Fruits.Enabled = true;
}
我需要禁用并在 CheckBoxList 中检查值为 1、4 和 5 的项目。
请帮帮我,先谢谢了。
您没有设置您设置 Enabled
属性 的列表项的 Selected
属性。
这是代码。您将迭代 CheckBoxList。如果当前复选框的值在 FruitsDB 中,那么它将被选中并禁用。
var memory = FruitsDB.ToString();
foreach (ListItem item in Fruits.Items)
{
if (memory.Contains(item.Value))
{
item.Enabled = false;
item.Selected = true;
}
}
我已经在 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>
在此 CheckBoxList
中可以 select 多个值,例如:
并且这个值被存储在数据库的字段中table:
for (int i = 0; i < Fruits.Items.Count; i++)
{
if (Fruits.Items[i].Selected == true)
{
FruitsUpdate += Fruits.Items[i].Text.ToString() + ";";
}
}
数据库中存储的值:1;4;5;
现在在 aspx 表单页面中,我需要禁用并检查存储在数据库中的 CheckBoxList
的值,但我尝试此代码但没有成功,因为所有 ListItem
都被禁用但未检查。
FruitsDB = dr["Fruits"].ToString();
if (FruitsDB.ToString() != "")
{
Fruits.Text = FruitsDB.ToString();
Fruits.Enabled = false;
}
else
{
Fruits.Enabled = true;
}
我需要禁用并在 CheckBoxList 中检查值为 1、4 和 5 的项目。
请帮帮我,先谢谢了。
您没有设置您设置 Enabled
属性 的列表项的 Selected
属性。
这是代码。您将迭代 CheckBoxList。如果当前复选框的值在 FruitsDB 中,那么它将被选中并禁用。
var memory = FruitsDB.ToString();
foreach (ListItem item in Fruits.Items)
{
if (memory.Contains(item.Value))
{
item.Enabled = false;
item.Selected = true;
}
}