Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / ASP.NET / General / September 2007

Tip: Looking for answers? Try searching our database.

CheckBoxList loses ListItems after postback

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
nothingsoriginalontheinternet@gmail.com - 09 Sep 2007 19:06 GMT
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
Eliyahu Goldin - 09 Sep 2007 22:05 GMT
Re-create the listitems on every postback in Page_PreInit event. Then they
will get loaded with correct values in Page_Load event.

Signature

Eliyahu Goldin,
Software Developer & Consultant
Microsoft MVP [ASP.NET]
http://msmvps.com/blogs/egoldin

> Hi. I'm having a lot of trouble with a CheckBoxList control that has
> its ListItems added to it dynamically. During PostBack, its ListItems
[quoted text clipped - 45 lines]
> Thanks,
> Mike P II
nothingsoriginalontheinternet@gmail.com - 09 Sep 2007 23:55 GMT
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

On Sep 9, 6:07 pm, "Eliyahu Goldin"
<REMOVEALLCAPITALSeEgGoldD...@mMvVpPsS.org> wrote:
> Re-create the listitems on every postback in Page_PreInit event. Then they
> will get loaded with correct values in Page_Load event.
[quoted text clipped - 3 lines]
> Software Developer & Consultant
> Microsoft MVP [ASP.NET]http://msmvps.com/blogs/egoldin
Eliyahu Goldin - 10 Sep 2007 07:31 GMT
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.

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
nothingsoriginalontheinternet@gmail.com - 10 Sep 2007 22:51 GMT
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

Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.