gpt4 book ai didi

c# - 显示动态复选框的现有选择

转载 作者:行者123 更新时间:2023-11-28 09:12:57 25 4
gpt4 key购买 nike

我正在动态创建复选框,并想要显示现有选择或想要动态检查它们。这是创建复选框的代码

    private void Role()
{
SystemUserDal dal = new SystemUserDal();
var roles = dal.GetRolesList();
foreach (KeyValuePair<Guid, string> r in roles)
{

CheckBox chk = new CheckBox();
chk.ID = r.Value;
chk.Text = r.Value;
rolepanel.Controls.Add(chk);
}
}

我有

      protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
SystemUserDal dal = new SystemUserDal();
var userID = Guid.Parse(Request.QueryString["ID"].ToString());
var user = dal.getSystemUserByID(userID);
if (user != null)
{
Role();
var role = user.Roles;


textuserid.Text = user.UserId.ToString();

user.Roles 拥有分配给特定用户的所有 Angular 色。我如何标记动态创建的复选框?

我尝试过这个,但仍然不起作用,因为 Angular 色有多个值

    private void Role(string role)
{
SystemUserDal dal = new SystemUserDal();
var userId = Guid.Parse(Request.QueryString["ID"].ToString());
var roles = dal.GetRolesList(userId);
foreach (KeyValuePair<Guid, string> r in roles)
{

CheckBox chk = new CheckBox();
chk.ID = r.Value;
chk.Text = r.Value;
if (role == r.Value)
{
chk.Checked = true;

} rolepanel.Controls.Add(chk);
}
}

最佳答案

可能重写你的Roles()功能

foreach (KeyValuePair<Guid, string> r in roles)
{

CheckBox chk = new CheckBox();
chk.ID = r.Value;
chk.Text = r.Value;

if(User.IsInRole(r.Value))
chk.Checked = true;

rolepanel.Controls.Add(chk);
}

更好的选择是使用 <asp:CheckBoxList />控制

<asp:CheckBoxList ID="RolesList" runat="server">
</asp:CheckBoxList>

隐藏代码

for (int i = 0; i < RolesList.Items.Count; i++)
{
if (User.IsInRole(RolesList.Items[i].Value))
RolesList.Items[i].Selected = true;
}

关于c# - 显示动态复选框的现有选择,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16070939/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com