CheckBoxList loses ListItems after postback

  • Thread starter nothingsoriginalontheinternet
  • Start date
N

nothingsoriginalontheinternet

Hi. I'm having a lot of trouble with a CheckBoxList control that has
its ListItems added to it dynamically. During PostBack, its ListItems
are no longer there. I don't need the ListItems to be visible after it
posts back because the user is redirected anyway, but I need access to
them programmatically.

I tried playing with viewstate things to try to get it to work, but
all that accomplished was refilling the list as it was before the user
checked and unchecked things when it posts back.

It is filled and read with the following code:

Sub Page_Load(...)
'...
If Not Page.IsPostBack Then
Dim roleChk As ListItem
For Each role As String In Roles.GetAllRoles
roleChk = New ListItem(role)
If Roles.IsUserInRole(mUser.UserName, role) Then
roleChk.Selected = True
End If
userRolesList.Items.Add(roleChk)
Next
'...
End If
End Sub

Sub saveUserBtn_Click(...)
'...
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.AddUserToRole(mUser.UserName,
userRolesList.Items(i).Text)
End If
ElseIf userRolesList.Items(i).Selected Then
Roles.RemoveUserFromRole(mUser.UserName,
userRolesList.Items(i).Text)
End If
Next
'...
End Sub

What am I doing wrong?

Thanks,
Mike P II
 
E

Eliyahu Goldin

Re-create the listitems on every postback in Page_PreInit event. Then they
will get loaded with correct values in Page_Load event.
 
N

nothingsoriginalontheinternet

Cool. I'm glad to be making some progress now, I've been dealing with
this for a while now. Thank you, Eliyahu.

I still have a problem, though. I'm using a code-behind file with a
partial class, so although it complains that the userRolesList control
doesn't exist when I try to add ListItems to it from Page_PreInit, it
doesn't like when I declare the control as a member of the page's
class because that control exists already in the other part of the
page's class.

So how is this normally done with partial classes?

-Mike P II
 
E

Eliyahu Goldin

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
re-define. At the best, you will manage to create another object which will
have nothing to do with the original one.

--
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.

I still have a problem, though. I'm using a code-behind file with a
partial class, so although it complains that the userRolesList control
doesn't exist when I try to add ListItems to it from Page_PreInit, it
doesn't like when I declare the control as a member of the page's
class because that control exists already in the other part of the
page's class.

So how is this normally done with partial classes?

-Mike P II
 
N

nothingsoriginalontheinternet

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"


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
re-define. At the best, you will manage to create another object which will
have nothing to do with the original one.

--
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.
I still have a problem, though. I'm using a code-behind file with a
partial class, so although it complains that the userRolesList control
doesn't exist when I try to add ListItems to it from Page_PreInit, it
doesn't like when I declare the control as a member of the page's
class because that control exists already in the other part of the
page's class.
So how is this normally done with partial classes?
-Mike P II
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,071
Latest member
MetabolicSolutionsKeto

Latest Threads

Top