Search code examples
c#asp.netcheckboxrepeater

ASP.NET repeater get all items and checkbox if user has right


I have an ASP.NET application and I would like to display all roles and add a checkbox which is checked if the user has the right. I use an ASP repeater to get all roles but how can I check the checkbox in the same repeater to get the user's role ? Here is my code:

  <asp:Repeater ID="RepeaterRole" runat="server" DataSourceID="ObjectDataSource2">
        <ItemTemplate>
            <div>
                <asp:CheckBox runat="server" Checked="False" />
                <asp:Label CssClass="lbl" ID="Label1" runat="server" Text='<%# Eval("RoleLabel")%>'></asp:Label>
            </div>
        </ItemTemplate>
    </asp:Repeater>
    <asp:ObjectDataSource ID="ObjectDataSource2" runat="server" SelectMethod="GetAllRolesToCollection" TypeName="Business.BusinessObject.Role"></asp:ObjectDataSource>

Solution

  •   <asp:Repeater ID="RepeaterRole" runat="server" DataSourceID="ObjectDataSource2">
            <ItemTemplate>
                <div>
                    <asp:CheckBox runat="server" Checked='<%# Convert.ToBoolean(Eval("role")) %>'  />
                    <asp:Label CssClass="lbl" ID="Label1" runat="server" Text='<%# Eval("RoleLabel")%>'></asp:Label>
                </div>
            </ItemTemplate>
        </asp:Repeater>
        <asp:ObjectDataSource ID="ObjectDataSource2" runat="server" SelectMethod="GetAllRolesToCollection" TypeName="Business.BusinessObject.Role"></asp:ObjectDataSource>
    

    Make sure you get 'role' field 1 or 0 from DB