我知道这个题目可能会有点怪,但我不知道如何准确地描述我的目标的CheckBoxList为字符串,保存到数据库,然后转换成数组
我试图做一些基于角色内容管理系统中的权限。我能想到的最好办法是从数据库中提取角色列表,并将它们放在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
感谢您的意见。我正在使用内置的成员资格,所以我的问题与用户对角色没有任何关系......我正在做的是将角色放在一个字符串中并将其附加到页面上。我想我可以做一对多的页面到角色的关系,但我只是觉得页表中的单个字段就足够了。但是,我不知道如何将数组存储在数据库中,而不是使用逗号分隔的字符串。 – 2010-02-10 19:19:06
昏暗值列表 =新名单()应作为新的列表(串) –
2010-02-10 19:36:00
我已经更新了代码尺寸值。 – tvanfosson 2010-02-10 19:37:29