根据其他列表选择复选框列表中的项目 [英] select the items of checkBox List according amother list

查看:52
本文介绍了根据其他列表选择复选框列表中的项目的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好

我有一个包含CheckBoxList的Web表单.
checkBoxList从表Role中获取数据.

我希望根据另一个列表(即List< int>角色
)选择checkBoxList项目
我正在尝试使用此代码,但问题是checkBoxList项的数量为0,因为它尚未从数据库中检索出来



Hi every body

I have a web form contain a CheckBoxList.
The checkBoxList gets data from table Role.

I want the checkBoxList items to be selected according another List (which is List<int> roles

I am trying to use this code but the problem is that the number of checkBoxList items is 0 becuase it is not retrive from the databse yet



foreach (int r in user.Roles)
          {

              foreach (ListItem listItem in chkListRoles.Items)
               {
                   if (listItem.Value.Equals(r.ToString()))
                       listItem.Selected = true;
               }
          }

推荐答案

我怀疑这意味着您随后在页面加载事件中对此有所了解.在prerender事件中执行此操作,如果所有其他操作均失败,请修复代码,以便在需要之前从数据库加载所需的数据.对我来说似乎很明显,您需要先加载数据,然后才能使用它.
I suspect this means you''re dong this in your page load event then. Do it in your prerender event, and, if all else fails, fix your code so it loads the data you need from the DB before you need it. It seems obvious to me that you need to load your data before you can use it.


这篇关于根据其他列表选择复选框列表中的项目的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆