Hiding a control’s commandfield button at runtime

Hiding a command button at runtime can be archieved by casting the commandfield in the DataBound event, and accessing the buttons from there. The sample below shows the hiding of a button based on a security role.

protected void DetailsViewDocuments_DataBound(object sender, EventArgs e)
{
      // Check role
      string currentuser = Common.GetRequestUser(); // some user retrieval process
      if (!Shared.API.Permissions.IsSomeRole(currentuser, _database)) // permissions check
      {
           // disable edit and delete buttons
           CommandField cmd = (CommandField)DetailsViewDocuments.Fields[6];
           cmd.ShowEditButton = false;
           cmd.ShowDeleteButton = false;
      }
}

Note that this is only a visual protection, and security through obscurity only never stands. You still need to handle security also in your actual processing.