我知道这个主题可能有点奇怪,但我不确定如何准确描述我的目标
我正在尝试在内容管理系统中执行一些基于角色的权限。我能想到的最好方法是从数据库中提取角色列表并将它们放在 CheckBoxList 中。从那里,我将检查值的逗号分隔字符串保存到数据库的页面详细信息中。最后,当页面被拉回时,我通过拉下逗号分隔的字符串并将其拆分为 String() 并循环遍历值来检查当前用户是否具有权限。
我担心的是,当我遍历 CheckBoxList 并将值转换为字符串时,循环方法会在字符串的末尾添加一个逗号......然后我必须在将逗号保存到数据库之前去掉它。
我的问题是,有没有更好的方法来做到这一点,这样我就不必在保存之前返回字符串并去除尾随逗号?
''# this is to get all selected items from
''# a checkboxlist and add it to a string
''# the reason for this is to insert the final string
''# into a database for future retrieval.
Dim i As Integer
Dim sb As StringBuilder = New StringBuilder()
For i = 0 To CheckBoxList1.Items.Count - 1
If CheckBoxList1.Items(i).Selected Then
sb.Append(CheckBoxList1.Items(i).Value & ",")
End If
Next
''# now I want to save the string to the database
''# so I first have to strip the comma at the end
Dim str As String = sb.ToString.Trim(",")
''# Then save str to the database
''# I have now retrieved the string from the
''# database and I need to now add it to a One Dimensional String()
Dim str_arr() As String = Split(str, ",")
For Each s As String In str_arr
''# testing purposes only
Response.Write(s & " -</br>")
''# If User.IsInRole(s) Then
''# Do Stuff
''# End If
Next