1

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>
Fred Smith
  • 2,047
  • 3
  • 25
  • 36

2 Answers2

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

Hardik
  • 228
  • 1
  • 6
0

Thanks Hardik, based on your solution, I created a similar method like this: public Dictionary GetAllRolesFromUserToDictionary() so the checkbox will bind to the boolean value Then the ASP code is like this:

 <asp:Repeater ID="Repeater1" runat="server" DataSourceID="ObjectDataSource3">
            <ItemTemplate>
                <div>
                    <asp:CheckBox ID="CheckBox2" runat="server" Checked='<%# Eval("Value")%>' />
                    <asp:Label ID="Label1" runat="server" Text='<%# Eval("Key")%>'></asp:Label>
                </div>
            </ItemTemplate>
        </asp:Repeater>
        <asp:ObjectDataSource ID="ObjectDataSource3" runat="server" SelectMethod="GetAllRolesFromCurrentUserToDictionary" TypeName="BusinessObject.RoleCollection"></asp:ObjectDataSource>
Fred Smith
  • 2,047
  • 3
  • 25
  • 36