如何通过从数据库中检索数据来检查/取消选中复选框列表

问题描述:

SqlCommand cmdPickAllAccessebility = DBManager.DataAccess.command();
            
cmdPickAllAccessebility.Parameters.Add(new SqlParameter("@option", SqlDbType.VarChar, 50));
cmdPickAllAccessebility.Parameters["@option"].Value = "PickAllAccessebility";

cmdPickAllAccessebility.Parameters.Add(new SqlParameter("@User_identity", SqlDbType.VarChar, 50));
cmdPickAllAccessebility.Parameters["@User_identity"].Value = rbtnForSelectUser.SelectedValue.ToString();//rbtnForSelectUser.SelectedValue //Session["User_ID"];

string sqlquery2 = "SchoolProc";
DataSet dsetPickAllAccessebility = DBManager.DataAccess.getdata(sqlquery2);

for (int i = 0; i < CheckBoxListForSelectingPrivileges.Items.Count; i++)
{
for (int j = 0; j < dsetPickAllAccessebility.Tables[0].Rows.Count; j++)
{
if (dsetPickAllAccessebility.Tables[0].Rows[j].ToString() == CheckBoxListForSelectingPrivileges.Items[i].Value)
{
CheckBoxListForSelectingPrivileges.Items[i].Selected = true;
}
else
{
CheckBoxListForSelectingPrivileges.Items[i].Selected = false;
}

}
}



这是我从数据库中检索数据并检查复选框列表的代码

但我没有得到任何输出

i会清楚地解释我的问题



i我有一个页面叫做管理页面。在该页面中我们可以看到单选按钮列表中的所有用户(将所有用户绑定到单选按钮列表)和复选框列表中的所有权限(绑定所有权限以复选框列表列表)



选择特定用户并通过选中将保存在表User_Access中的复选框列表为该用户选择特定权限



注册表


this was my code for retrieving the data from database and checked the check box list
but i am not getting any output
i will clearly explain about my problem

i am having a page called administration page.in that page we can see all the user in radio button list(binded all the users to radio button list) and all the privileges in check box list (binded all the privileges to check box list list)

by selecting particular user and selecting particular privileges to that user by checking the check box list that will save in table "User_Access" like below

registration table

user_id | login_id | password 
1       |nitish    | ******



特权表


privileges table

access_id | Description | status
1         | admission   |  true
2         | events      |  true
3         |fee-collection| true



User_Access表


User_Access table

user_id |access_id
1       | 2
1       | 3



就像上面的表我正在保存....



如果下次我的问题是什么我选择了同一个用户,我希望通过从User_Access表中检索数据来查看所有准备好的权限



这里我使用access_id作为数据值字段复选框列表。

数据集我得到的access_id被称为复选框列表的数据值字段

现在我想要的是根据user_access表当我们在admin页面中选择user_id 1它应该显示复选框列表,复选框2,3应该被选中,1应该被取消选中


like the above table i am saving....

what was my problem if next time i selected the same user i want to see the privileges what all ready he is having by retrieving the data from User_Access table

here i am using access_id as data value field for check box list.
in data set i am getting the access_id called as the data value fields of check box list
now what i want is according to the user_access table when we select user_id 1 in admin page it should show the check box list with check box 2,3 should be checked and 1 should be unchecked

首先,阅读我的评论。



如果我理解你并且 dsetPickAllAccessebility.Tables [0] .Rows 等于 CheckBoxListForSelectingPrivileges.Items.Count ,您应该根据 dsetPickAllAccessebility.Tables [i]更改 Selected 属性.Rows [ N] .Item [status] value。

First of all, read my comment.

If i understood you well and dsetPickAllAccessebility.Tables[0].Rows is equal to CheckBoxListForSelectingPrivileges.Items.Count, you should change the Selected property based on dsetPickAllAccessebility.Tables[i].Rows[N].Item["status"] value.
for (int j = 0; j < dsetPickAllAccessebility.Tables[0].Rows.Count; j++)
{
    CheckBoxListForSelectingPrivileges.Items[j].Selected = dsetPickAllAccessebility.Tables[0].Rows[j].Item["status"].Value;
}



你不需要第二次循环。


You don''t need the second loop.


嗨亲爱的



Plz试试这个。

Hi Dear

Plz try this.
string userId = Session["UserId"].ToString();
            foreach (ListItem item in this.chkList.Items)
            {
                if (item.Value != userId)
                {
                    item.Selected = true;
                }
            }


试试这个:

Try this:
//Clear all the selected items
for (int i = 0; i < CheckBoxListForSelectingPrivileges.Items.Count; i++)
{
CheckBoxListForSelectingPrivileges.Items[i].Selected = false;
}
//Iterate all the items and compare them to the returned result set
for (int i = 0; i < CheckBoxListForSelectingPrivileges.Items.Count; i++)
{
for (int j = 0; j < dsetPickAllAccessebility.Tables[0].Rows.Count; j++)
{
if (dsetPickAllAccessebility.Tables[0].Rows[j]["columnNameOrIndex"].ToString() == CheckBoxListForSelectingPrivileges.Items[i].Value)
{
CheckBoxListForSelectingPrivileges.Items[i].Selected = true;
break;//break the loop if item is matched.
}
//No need for an else block.
}
}