CheckBoxList loses ListItems after postback

Discussion in 'ASP .Net' started by nothingsoriginalontheinternet@gmail.com, Sep 9, 2007.

  1. Guest

    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
    , Sep 9, 2007
    #1
    1. Advertising

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

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


    <> wrote in message
    news:...
    > 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, Sep 9, 2007
    #2
    1. Advertising

  3. Guest

    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"
    <> wrote:
    > Re-create the listitems on every postback in Page_PreInit event. Then they
    > will get loaded with correct values in Page_Load event.
    >
    > --
    > Eliyahu Goldin,
    > Software Developer & Consultant
    > Microsoft MVP [ASP.NET]http://msmvps.com/blogs/egoldin
    , Sep 9, 2007
    #3
  4. 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


    <> wrote in message
    news:...
    > 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"
    > <> wrote:
    > > Re-create the listitems on every postback in Page_PreInit event. Then

    they
    > > will get loaded with correct values in Page_Load event.
    > >
    > > --
    > > Eliyahu Goldin,
    > > Software Developer & Consultant
    > > Microsoft MVP [ASP.NET]http://msmvps.com/blogs/egoldin

    >
    Eliyahu Goldin, Sep 10, 2007
    #4
  5. Guest

    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"
    <> 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
    > 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
    >
    > <> wrote in message
    >
    > news:...
    >
    > > 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"
    > > <> wrote:
    > > > Re-create the listitems on every postback in Page_PreInit event. Then

    > they
    > > > will get loaded with correct values in Page_Load event.

    >
    > > > --
    > > > Eliyahu Goldin,
    > > > Software Developer & Consultant
    > > > Microsoft MVP [ASP.NET]http://msmvps.com/blogs/egoldin
    , Sep 10, 2007
    #5
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Eric Robishaw
    Replies:
    1
    Views:
    4,326
    Victor Garcia Aprea [MVP]
    Dec 27, 2003
  2. Boris
    Replies:
    1
    Views:
    2,897
    binusworld
    Sep 30, 2008
  3. Nathan Sokalski
    Replies:
    1
    Views:
    677
    bruce barker
    Mar 19, 2008
  4. Amelyan
    Replies:
    0
    Views:
    214
    Amelyan
    Apr 17, 2005
  5. Nathan Sokalski
    Replies:
    1
    Views:
    163
    bruce barker
    Mar 19, 2008
Loading...

Share This Page