我有一个CheckBoxList
,我循环遍历所有ListItems
并查看是否有其中任何一个被检查过。
奇怪的是,虽然只检查了其中一个,但它认为所有这些都已经过检查。
C#代码:
foreach (ListItem item in cblPermissions.Items)
{
if (item.Selected)
{
// Even though it's not checked on the HTML page - This code still runs.
}
}
这是我绑定数据的方式:
private void BindPermissions()
{
var roles =
RoleInfoProvider.GetAllRoles(
string.Format("RoleName like 'EmployerAdmin[_]%' AND SiteID = {0} AND RoleName NOT LIKE '%Super_User%'",
CMSContext.CurrentSiteID));
cblPermissions.DataSource = roles;
cblPermissions.DataBind();
var userInfo = UserInfoProvider.GetUserInfoByGUID(QueryHelper.GetGuid("guid", Guid.Empty));
if (userInfo != null)
{
var isSuperUser = userInfo.IsInRole(Constants.EmployerAdminSuperUserRole, CMSContext.CurrentSiteName);
if (!isSuperUser)
{
var userRoles = UserInfoProvider.GetUserRoles(userInfo);
foreach (DataRow row in userRoles.Rows)
{
var roleName = row["RoleName"].ToString();
var roleItem = cblPermissions.Items.FindByValue(roleName);
if (roleItem != null)
{
roleItem.Selected = true;
}
}
}
else
{
MarkAllPermissionsAsChecked();
}
}
}
HTML:
<asp:CheckBoxList runat="server" ID="cblPermissions" DataValueField="RoleName" DataTextField="RoleDescription"/>
为什么会发生这种情况?