Monday, 26 January 2009

Disallow Navigation on ForeignKey FieldTemplate – Dynamic Data

This article originates from a post I ding on the ASP.NET Dynamic Data Forum here Re: A few problems with my dynamic data website, Rick Anderson suggesting that it would be a useful post to link to in his FAQ. So here it is for easy access rather than having to search the forum for it.

HappyWizard

Firstly the idea is that you want to show the name of the entity but not link to if from the ForeignKey FieldTemplate.

Category has Navigation disabled

Figure 1 - Category has Navigation disabled

The code we need for this comes in three parts

  1. The Attribute
  2. The modification the the FieldTemplate
  3. The Metadata.

The Attribute

[AttributeUsage(AttributeTargets.Property)]
public class AllowNavigationAttribute : Attribute
{
    public Boolean Show { get; private set; }

    public AllowNavigationAttribute(Boolean show)
    {
        Show = show;
    }
    // this will allow us to have a default set to false
    public static AllowNavigationAttribute Default = new 
AllowNavigationAttribute
(true); }

Listing 1 – AllowNavigation attribute

Note the use of the Default see Writing Attributes and Extension Methods for Dynamic Data for the details on why.

The modification the the FieldTemplate

Now we need to add a little change to the ForeignKey.aspx.cs file which can be found in the ~/DynamicData/FieldTemplates folder of your Dynamic Data site.

protected string GetNavigateUrl()
{
    var allow = Column.Attributes.OfType<AllowNavigationAttribute>()
                .DefaultIfEmpty(new AllowNavigationAttribute(true))
                .FirstOrDefault().Show;
    if (!AllowNavigation || !allow)
    {
        return null;
    }

    if (String.IsNullOrEmpty(NavigateUrl))
    {
        return ForeignKeyPath;
    }
    else
    {
        return BuildForeignKeyPath(NavigateUrl);
    }
}

Listing 2 – ForeignKey FieldTemplate modifications

All we’ve done here is get the attribute into the allow variable and the add it to the checks in the if statement so that:

  • If the internal AllowNavigation is set then null is returned.
  • If the custom attribute is set null is returned.

The Metadata

[MetadataType(typeof(ProductMD))]
public partial class Product
{
    public partial class ProductMD
    {
       //EntityRef
        [AllowNavigation(false)]
        public object Category {get;set;}
    }
}

Listing 3 – sample Metadata

Now if you attribute a foreign key column up with the AllowNavigationAttribute you can turn off the hyperlink.

Happy Coding and remember You’re a PC HappyWizard

16 comments:

Anonymous said...

Hei,

where should I write the first part(The Attribute)?Metadata,...?

Stephen J. Naughton said...

That depends, I always asume a website not a Web Application Project, you put in a class file:
Website: ~/App_Code
Web App: anywhere - but remember it must have a namespace here.

Steve :D

Anonymous said...

Hi,

It works very well, the link is disabled. But the ForeignKey FieldTemplate still become underlined and blue when the mouse went on it! Please, how can I correct this?

Stephen J. Naughton said...

I'll look into it e-mail me direct as I'm watching Tech.days till late tonight :D my e-mail is top right of the page.

Steve

Stephen J. Naughton said...

Here's the new post http://csharpbits.notaclue.net/2009/04/disallow-navigation-on-foreignkey.html sadly LiveWriter only allows me to post a new version :(

Steve :D

Anonymous said...

Anybody have a VB version of this?

Stephen J. Naughton said...

I woudl do search for a C# to VB converter and convert the listings and then paste them into a VB project.

Steve

Anonymous said...

Tried that but could not get it to work :(

Stephen J. Naughton said...

I'll look at doing a VB convetion I will post the code along side the C# here or in a new article (Livew writer sometime posts to a new article if the article I'm updating is very old)

Steve :D

Anonymous said...

Tried to get it to work with .Net 4.0... No go!

Only spent about 30 Minutes looking into issue. I suspect this is a case of outdated information, where information needs to be slightly tweaked to work for next version.

Stephen J. Naughton said...

You are right there will be some minor issue between DD1 and DD4

Steve

Unknown said...

Hi Steve,

I tried your above code, but the hyperlink doesn't get disabled in edit mode.

How can we disable the foreign key hyperlink during edit?

Please help.

Thanks
Rag

Stephen J. Naughton said...

Hi Raghu, I'm away at the moment so send me an e-mail reminding me about this and I'll test and fix when I get back.

Steve :)

Unknown said...

Hi Steve,
Just a gentle reminder about my previous question on disabling hyperlinks in edit mode.

Thanks,
Raghu

Stephen J. Naughton said...

still away sorry :)

Steve

Stephen J. Naughton said...

Hi raghu, I'm back now an will have a look as soon as I get a moment.

Steve