Articles in this Series
- Introduction - A DynamicData Attribute Based Permission Solution using User Roles.
- Part 1 - Permissions Attribute (Metadata) Classes.
- Part 2 - Sample Metadata for project.
- Part 3 - The Helper Extension Methods.
- Part 4 - Limit Tables shown on Default page and List, Edit & Details etc.
- Part 5 - Generate Columns/Rows (using IAutoFieldGenerator)
- Part 6 - Miscellaneous bits
- Part 7 - Updating the ListDetails Page
- DynamicData - Limit the Filter Fields
- DynamicData - Automatic Column Update
Disabling Insert hyperlink on List and Details etc
Listing 1 shows the code to add to pages to disable the insert button.
// get user permissions var tablePermissions = table.GetTablePermissions(Roles.GetRolesForUser()); // Disable insert hyperlink in inserts denied. if (tablePermissions.Contains(TablePermissionsAttribute.Permissions.DenyInserts)) { InsertHyperLink.Visible = false; }
Listing 1
Removing; Edit, Delete, Details hyperlinks from Lists
Listing 2 is added to the List page as class variables and Listing 3 is added to the Page_Load, it will remove the whole column if all three class variables are true.
protected Boolean denyEdit = false; protected Boolean denyDelete = false; protected Boolean denyDetails = false;
// get user permissions var tablePermissions = table.GetTablePermissions(Roles.GetRolesForUser()); // get status of links for column 0 from table permissions if (tablePermissions.Contains(TablePermissionsAttribute.Permissions.DenyEdit)) denyEdit = true; if (tablePermissions.Contains(TablePermissionsAttribute.Permissions.DenyDelete)) denyDelete = true; if (tablePermissions.Contains(TablePermissionsAttribute.Permissions.DenyDetails)) denyDetails = true;
// if all buttons are hidden the hide the column if (denyDelete && denyDetails && denyEdit) { GridView1.Columns.RemoveAt(0); // remove the event handler if not needed GridView1.RowDataBound -= GridView1_RowDataBound }
To remove individual hyperlinks an OnRowDataBound event handler needs to be added see Listing 4.
<asp:GridView ID="GridView1" runat="server" DataSourceID="GridDataSource" AllowPaging="True" AllowSorting="True" CssClass="gridview" OnRowDataBound="GridView1_RowDataBound">
The same class variables are used here to turn off individual hyperlinks (
// manage security on Edit Delete links protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e) { // don't bother to hide individual links if they are all hidden if (e.Row.RowType == DataControlRowType.DataRow)// && !(!denyDelete && !denyDetails && !denyEdit)) { var controls = e.Row.Cells[0].Controls; if (controls.Count > 0) { foreach (var c in controls) { if (c.GetType() == typeof(HyperLink)) { var h = c as HyperLink; if ((h.Text.ToLower() == "edit" && denyEdit) || (h.Text.ToLower() == "details" && denyDetails)) { ((HyperLink)c).Visible = false; } } else if (c.GetType() == typeof(LinkButton)) { var h = c as LinkButton; if (h.Text.ToLower() == "delete" && denyDelete) { ((LinkButton)c).Visible = false; } } } } } }
Note: if all three class variable are true you could add the && !(!denyDelete && !denyDetails && !denyEdit) to the if statement and remover the GridView1.RowDataBound -= GridView1_RowDataBound line from Page_Load if statement, if you you still need to process the rows for some other reason.
Next the ListDetailsPage.
2 comments:
Great series!
An easier way to remove individual hyperlinks is to bind the Visible attribute of the links to the code behind variables like so...
Visible="<%# !denyEdit %>"
Great idea I will certanly use it in the near future.
Steve :D
Post a Comment