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.
Firstly the idea is that you want to show the name of the entity but not link to if from the ForeignKey FieldTemplate.
Figure 1 - Category has Navigation disabled
The code we need for this comes in three parts
- The Attribute
- The modification the the FieldTemplate
- 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
16 comments:
Hei,
where should I write the first part(The Attribute)?Metadata,...?
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
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?
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
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
Anybody have a VB version of this?
I woudl do search for a C# to VB converter and convert the listings and then paste them into a VB project.
Steve
Tried that but could not get it to work :(
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
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.
You are right there will be some minor issue between DD1 and DD4
Steve
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
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 :)
Hi Steve,
Just a gentle reminder about my previous question on disabling hyperlinks in edit mode.
Thanks,
Raghu
still away sorry :)
Steve
Hi raghu, I'm back now an will have a look as soon as I get a moment.
Steve
Post a Comment