
Signature
Eliyahu Goldin,
Software Developer & Consultant
Microsoft MVP [ASP.NET]
http://msmvps.com/blogs/egoldin
> Cool. I'm glad to be making some progress now, I've been dealing with
> this for a while now. Thank you, Eliyahu.
[quoted text clipped - 19 lines]
> > Software Developer & Consultant
> > Microsoft MVP [ASP.NET]http://msmvps.com/blogs/egoldin
Finally! I figured out the rest of the way...
Putting the data in during Page's PreInit seems like it should have
worked, but it wasn't working out for me because I guess ASP.net
hadn't yet gone through the control tree from the web form so there
was no CheckBoxList to manipulate at that time. I couldn't get it to
work by populating it by codebehind.
What I found to work instead is using an ObjectDataSource control to
fill the CheckBoxList right in the web form, but of course the check
boxes would not be properly checked because there doesn't seem to be a
"DataSelectedField" on the CheckBoxList control (shouldn't there be
one?) as are "DataTextField" and "DataValueField" properties. In my
case this was an option because my CheckBoxList is a choice of the
Roles, and I could just do this:
<asp:CheckBoxList runat="server" ID="userRolesList"
DataSourceID="rolesData" />
<asp:ObjectDataSource runat="server" ID="rolesData"
TypeName="System.Web.Security.Roles" SelectMethod="GetAllRoles" />
I check the boxes in Page_Load() (if not postback):
userRolesList.DataBind()
Dim i As Integer
For i = 0 To userRolesList.Items.Count - 1
If Roles.IsUserInRole(mUser.UserName, userRolesList.Items(i).Text)
Then
userRolesList.Items(i).Selected = True
End If
Next
And I have to force it to DataBind() here because at this point in the
life cycle the CheckBoxList Items were otherwise empty (is that
normal?). I don't need to check the items when it's not postback
because they are just required for the user to see what they are
currently.
Doing these things made it behave like I think it should, in my
button's Click event I can do this now:
Dim i As Integer
For i = 0 To userRolesList.Items.Count - 1
If Roles.IsUserInRole(mUser.UserName, userRolesList.Items(i).Text)
Then
If Not userRolesList.Items(i).Selected Then
Roles.RemoveUserFromRole(mUser.UserName,
userRolesList.Items(i).Text)
End If
ElseIf userRolesList.Items(i).Selected Then
Roles.AddUserToRole(mUser.UserName, userRolesList.Items(i).Text)
End If
Next
But in the end, I don't think all of this was necessary. I've done a
lot of searching on Google and I could not locate any mention of
working with dynamically populated CheckListBox's in a way that
required this. There must be something else wrong with my code.
I'm using ASP.net 2, maybe that is relevant. I have tried some example
code that achieves a similar thing, and it does not work running out
of the Visual Studio 2005 testing server on my computer.
So does anyone have an explanation as to why all of this trouble in
just my case? Or are steps like these a well-kept secret? And, to
answer your question, Eliyahu Goldin, the compiler was the one with
the problems when I'd declare the control additionally by declaring it
as a class member in my code-behind file, but the problem with the
userRolesList control not existing was an exception thrown during
runtime ("object reference not set to instance of an object").
userRolesList was defined in the web form by adding the
<asp:CheckBoxList /> element, and I was trying to put it as a class
member by declaring it like: "Public userRolesList As CheckBoxList"
On Sep 10, 3:35 am, "Eliyahu Goldin"
<REMOVEALLCAPITALSeEgGoldD...@mMvVpPsS.org> wrote:
> Is that the compiler who is complaining that the userRolesList control
> doesn't exist? How do you define userRolesList? In any case you shouldn't
[quoted text clipped - 34 lines]
> > > Software Developer & Consultant
> > > Microsoft MVP [ASP.NET]http://msmvps.com/blogs/egoldin