Wednesday, 28 May 2008

DynamicData - Miscellaneous bits - Part 6

Articles in this Series

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;

Listing 2

// 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
}

Listing 3

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

Listing 4

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;
                    }
                }
            }
        }
    }
}

Listing 5

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:

Anonymous said...

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 %>"

Steve said...

Great idea I will certanly use it in the near future.

Steve :D