J
jeremy
Had a tough time figuring this one out and couldn't find a good
solution, so I thought I would post this and hopefully it will help
someone out.
When using DataBind to dynamically bind a list to the CheckBoxList
control in ASP .NET, I had a hard time figuring out how to dynamically
select the items that needed to be selected. Specifically, I was
displaying checkboxes for Roles, and I wanted to check the boxes of
the roles that were currently assigned to the user.
I used the OnDataBound event in my CheckBoxList to call a selectBoxes
method. In my selectBoxes method, I was able to get the collection of
Items in the CheckBoxList and loop through them, selecting the ones
that needed to be selected.
Here is my CheckBoxList tag in the .aspx file:
<asp:CheckBoxList ID="rolesCheckBoxes" runat="server"
DataSourceID="getRolesByUserName"
DataTextField="RoleName"
DataValueField="RoleName"
OnDataBound="selectBoxes">
</asp:CheckBoxList>
And here is the method selectBoxes that gets called OnDataBound in the
code behind:
public void selectBoxes(object sender, EventArgs e) {
//call this out here instead of Roles.isUserInRole
//because isUserInRole makes a DB call each time
string[] roles = Roles.GetRolesForUser("username");
foreach (ListItem l in ((CheckBoxList)sender).Items) {
foreach (string role in roles) {
if (role == l.Text) {
l.Selected = true;
}
}
}
}
The key for me was to cast the 'sender' variable as a CheckBoxList
object, and then use the Items property to loop through each
individual checkbox in the list. Sweet!
Jeremy
solution, so I thought I would post this and hopefully it will help
someone out.
When using DataBind to dynamically bind a list to the CheckBoxList
control in ASP .NET, I had a hard time figuring out how to dynamically
select the items that needed to be selected. Specifically, I was
displaying checkboxes for Roles, and I wanted to check the boxes of
the roles that were currently assigned to the user.
I used the OnDataBound event in my CheckBoxList to call a selectBoxes
method. In my selectBoxes method, I was able to get the collection of
Items in the CheckBoxList and loop through them, selecting the ones
that needed to be selected.
Here is my CheckBoxList tag in the .aspx file:
<asp:CheckBoxList ID="rolesCheckBoxes" runat="server"
DataSourceID="getRolesByUserName"
DataTextField="RoleName"
DataValueField="RoleName"
OnDataBound="selectBoxes">
</asp:CheckBoxList>
And here is the method selectBoxes that gets called OnDataBound in the
code behind:
public void selectBoxes(object sender, EventArgs e) {
//call this out here instead of Roles.isUserInRole
//because isUserInRole makes a DB call each time
string[] roles = Roles.GetRolesForUser("username");
foreach (ListItem l in ((CheckBoxList)sender).Items) {
foreach (string role in roles) {
if (role == l.Text) {
l.Selected = true;
}
}
}
}
The key for me was to cast the 'sender' variable as a CheckBoxList
object, and then use the Items property to loop through each
individual checkbox in the list. Sweet!
Jeremy