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