复选框列表转换为字符串,保存到数据库,然后转换为数组

发布于 2024-08-21 04:24:40 字数 1392 浏览 6 评论 0原文

我知道这个主题可能有点奇怪,但我不确定如何准确描述我的目标,

我正在尝试在内容管理系统中执行一些基于角色的权限。我能想到的最好方法是从数据库中提取角色列表并将它们放入 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

I know the subject might be a little strange, but I wasn't sure how to exactly describe my goal

I'm trying to do some Role based permissions in a content management system. The best way I can think of to do this is to pull the list of roles from the database and place them in a CheckBoxList. From there, I save a comma separated string of the checked values into the page details of the database. Finally when the page is pulled back up, I check if the current User has permission by pulling down the comma separated string and splitting it into a String() and loop through the values.

The concern I have is that when I loop through the CheckBoxList and convert the values to a string, the loop method adds a comma to the end of the string... I then have to strip the comma before saving it to the database.

My question is, is there a better way of doing this so that I don't have to go back into the string and strip the trailing comma before saving?

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

意中人 2024-08-28 04:24:40

将所有值放入字符串列表中,然后使用 String.Join 连接它们。我不会评论保留逗号分隔的列表而不是用户与角色的一对多关系。 :-)

Dim values as List(Of String) = New List(Of String)()
For i = 0 To CheckBoxList1.Items.Count - 1 
    If CheckBoxList1.Items(i).Selected Then 
        values.Add( CheckBoxList1.Items(i).Value )
    End If 
Next

Dim str As String = String.Join( ",", values.ToArray() )

Put all the values in a list of strings, then use String.Join to concatenate them. I won't comment on keeping a comma-separated list rather than a one-to-many relationship of users to roles. :-)

Dim values as List(Of String) = New List(Of String)()
For i = 0 To CheckBoxList1.Items.Count - 1 
    If CheckBoxList1.Items(i).Selected Then 
        values.Add( CheckBoxList1.Items(i).Value )
    End If 
Next

Dim str As String = String.Join( ",", values.ToArray() )
↙厌世 2024-08-28 04:24:40

您是否可以简单地拥有一个数据库表,其中包含对页面的引用(通过名称、路径或某些其他唯一 ID)以及对角色的引用?

我当然不是说这是最好的解决方案(无论如何),它只是一个示例:

角色表:

RoleId | RoleName
_________________
1      | Editor
2      | Administrator

页面表:

PageId | PageName
8      | ~/Page1.aspx
9      | ~/OtherPlace/Page2.aspx

页面角色表:

RoleId | PageId
2      | 8
2      | 9
1      | 9

然后您将数据存储在连接表中,并在访问页面时拉回您的数据基于当前页面的角色。当然,它根本不必基于页面,您可以轻松地将我的示例中的 Page 替换为 Permission。

Could you perhaps simply have a database table that contains a reference to the page (be it by name, path, or some other unique id), and a reference to the role?

I'm certainly not saying this is the best solution (by any means), it's simply an example:

Role Table:

RoleId | RoleName
_________________
1      | Editor
2      | Administrator

Page Table:

PageId | PageName
8      | ~/Page1.aspx
9      | ~/OtherPlace/Page2.aspx

PageRole Table:

RoleId | PageId
2      | 8
2      | 9
1      | 9

You would then store your data in the joining table, and when accessing the page pull back your roles based on the current page. Of course, it doesn't have to based on the page at all, you could just as easily replace Page in my example with Permission.

ぶ宁プ宁ぶ 2024-08-28 04:24:40

如果您使用 ASP.NET 成员身份,则可以为一个用户分配多个角色。

Roles.AddUserToRoles(username, roles);

Roles.IsUserInRole(role);

只是需要考虑的事情。

If you use ASP.NET membership, you can assign multiple roles to a user.

Roles.AddUserToRoles(username, roles);

Roles.IsUserInRole(role);

Just something to consider.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文