Controlling GridView command buttons based on Object Permissions

Controlling GridView command buttons based on Object Permissions

Old forum URL: forums.lhotka.net/forums/t/1794.aspx


jspurlin posted on Sunday, November 19, 2006

I noticed with the PTracker web example, hence my own implementation that although I may deny someone the right to Delete a record, if the user selects a record to update, the Delete command is available to them.

I have a tentative solution, but I have a question too:

Here is the GridView1_DataBound method to add a confirm to the delete button and then hide it if users do not have the permission to delete. This delete button will also be hidden when the user selects another row to edit.

if (e.Row.RowType == DataControlRowType.DataRow)
        {
            int index = e.Row.RowIndex;
            LinkButton lnkDelete = (LinkButton)(e.Row.Cells[this.GridView1.Columns.Count - 1].Controls[2]);
            if (lnkDelete != null)
            {
                string rowState = e.Row.RowState.ToString();
                if (rowState.CompareTo("Edit") != 0 && rowState.CompareTo("Alternate, Edit") != 0)
                {
                    //hide delete button in unselected rows
                    //show cancel button for update mode.
                    if (ResourceManager.Library.UsersBasic.CanDeleteObject())
                        lnkDelete.OnClientClick = "return confirm('Are you sure you want to delete this record?');";
                    else
                        lnkDelete.Visible = false;
                }
            }
        }

My question are there other ways to check RowState.  I did not find a strong typed value for the RowState. Will my implementation for webforms break, or may I rely that the string values for RowState will not change?

Copyright (c) Marimer LLC