4

I've a requirement to disable edit/create buttons in ListView.Could anyone please show me how to enable or disable Edit/Create buttons in ListView from code behind, please.

Not sure if this is possible.

<InsertItemTemplate>
    <tr>
        <td>
            <asp:Button ID="InsertButton" runat="server" CommandName="Insert" 
                Text="Insert" CssClass="button" 
                ValidationGroup="InsertValidation" CausesValidation="true" />
            <asp:Button ID="CancelButton" runat="server" CommandName="Cancel" 
                Text="Clear" CssClass="button" />
        </td>
    </tr>
</InsertItemTemplate>
Akram Shahda
  • 14,655
  • 4
  • 45
  • 65
Nil Pun
  • 17,035
  • 39
  • 172
  • 294

1 Answers1

4

Use RolePrincipal.IsInRole.

The code should be similiar to the following:

void listView_ItemDataBound(...)
{
    Button targetButton = (Button) e.Item.FindControl("TargetButtonName");

    targetButton.Enabled = User.IsInRole("Administrators");
}

Refer to how to enable and disable button based on user role?

Community
  • 1
  • 1
Akram Shahda
  • 14,655
  • 4
  • 45
  • 65
  • Thank you @Akram for all your help. If I knew you in-person I would have taken you for free coffee. Your time is much appreciated. – Nil Pun Jun 01 '11 at 23:05
  • @Akram, I tried Button targetInsertButton = (Button)e.Item.FindControl("InsertButton"); targetInsertButton.Enabled = false; It's giving Object Reference not set error. However EditButton works fine. – Nil Pun Jun 01 '11 at 23:16
  • @flybyte: If you posted your html markup, I will be able to help you with that ... – Akram Shahda Jun 02 '11 at 05:26
  • not sure what you meant. I tried this and not working :Button targetInsertButton = (Button)e.Item.FindControl("InsertButton"); targetInsertButton.Enabled = false; – Nil Pun Jun 02 '11 at 08:23
  • @flybyte: Insert button resides in the insert item .. you have to check the item type before trying to find the insert button .. – Akram Shahda Jun 02 '11 at 08:31
  • @@flybyte: Refer to http://stackoverflow.com/questions/5992172/how-to-enable-and-disable-button-based-on-user-role/5993873#5993873 – Akram Shahda Jun 02 '11 at 10:22
  • Thanks but its not going to work for me as my role is based on custom database i.e. Teamleader, supervisor etc. – Nil Pun Jun 02 '11 at 13:18
  • @flybyte: It is hard to tell what's going on on your code .. The provided info must help you figure it out. You still need to make some effort. Try debugging a little ... – Akram Shahda Jun 02 '11 at 13:23