Answered by:
Changing the display name of a field

Question
-
User-222364991 posted
Hi,
I'm using the april drop of the dynamic data preview stuff and am impressed with the good work done by the development team.
I've looked at the webcasts forums etc but can't find the answer to my question.
I would like to define my own fieldnames for displaying in listview, detailview, etc
e.g. In the Asset table I have a field called AssetTitle that I would like to display as Title.
I know that I can copy the list.aspx, detail.aspx, into custom page folders and turn autogenerate to false and define my own columns but I and have read about using "buddy" classes and wondered if there was some way to define the display names in a class rather than creating lots of copies of list.aspx, details.aspx etc.
Any help is appreciated
Sean
Wednesday, April 30, 2008 6:00 AM
Answers
-
User-797310475 posted
Hi Sean,
you can use System.ComponentModel.DisplayNameAttribute to achieve what you need.
- Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Wednesday, April 30, 2008 6:38 AM -
User660823006 posted
18 [MetadataClass(typeof(Asset_Metadata))]
19 partial class Asset
20 {
21 }
on line 18 if should be [MetadataType(typeof(Asset_Metadata))]
- Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Wednesday, April 30, 2008 12:09 PM -
User1641955678 posted
If you set the DisplayName on a field, that string should show up as the header of that column in the gridview (among other places). Maybe you are looking for the string somehere else?
David
- Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Thursday, May 1, 2008 10:55 AM
All replies
-
User-797310475 posted
Hi Sean,
you can use System.ComponentModel.DisplayNameAttribute to achieve what you need.
- Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Wednesday, April 30, 2008 6:38 AM -
User-222364991 posted
Thanks for your speedy reply.
Still having a few problems though (self generated no doubt)
I can get this to work if I change the Datacontext.designer.cs directly like below but that is less than ideal
1 [Column(Storage="_AssetTitle", DbType="NVarChar(255) NOT NULL", CanBeNull=false)] 2 [DisplayName("Title")] 3 public string AssetTitle 4 { 5 get 6 { 7 return this._AssetTitle; 8 } 9 set 10 { 11 if ((this._AssetTitle != value)) 12 { 13 this.OnAssetTitleChanging(value); 14 this.SendPropertyChanging(); 15 this._AssetTitle = value; 16 this.SendPropertyChanged("AssetTitle"); 17 this.OnAssetTitleChanged(); 18 } 19 } 20 }
however when I change this in my partial class like below
1 public partial class Asset 2 { 3 [DisplayName("Title")] 4 public string AssetTitle { get; set; } 5 }
I get the error
The type 'Asset' already contains a definition for 'AssetTitle'
so I tried this
1 using System; 2 using System.Collections.Generic; 3 using System.ComponentModel; 4 using System.Data; 5 using System.Data.Linq; 6 using System.Data.Linq.Mapping; 7 using System.Linq; 8 using System.Linq.Expressions; 9 using System.Reflection; 10 using System.Web.DynamicData; 11 12 13 14 /// <summary> 15 /// Summary description for Asset 16 /// </summary> 17 18 [MetadataClass(typeof(Asset_Metadata))] 19 partial class Asset 20 { 21 } 22 23 public class Asset_Metadata 24 { 25 [DisplayName("Title")] 26 public string AssetTitle { get; set; } 27 }
I then get the errors
The type or namespace name 'MetadataClass' could not be found (are you missing a using directive or an assembly reference?)
The type or namespace name 'MetadataClassAttribute' could not be found (are you missing a using directive or an assembly reference?)
I guess I am missing something.
Do I need to download the MVC Toolkit as well as the Dynamic data stuff.
Any help much appreciated
Sean
Wednesday, April 30, 2008 8:48 AM -
User660823006 posted
18 [MetadataClass(typeof(Asset_Metadata))]
19 partial class Asset
20 {
21 }
on line 18 if should be [MetadataType(typeof(Asset_Metadata))]
- Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Wednesday, April 30, 2008 12:09 PM -
User-1974009678 posted
Am having the same problem, code shown below:
1 public partial class testDataContext { 2 } 3 4 [MetadataType(typeof(Content_Metadata))] 5 public partial class Content { 6 } 7 8 public class Content_Metadata { 9 10 11 private object _c_title; 12 13 14 [DisplayName("title")] 15 [Description("Page title")] 16 public object c_title 17 { 18 get 19 { 20 return _c_title; 21 } 22 set 23 { 24 _c_title = value; 25 } 26 }
Thursday, May 1, 2008 9:13 AM -
User-1974009678 posted
Forgot to say, that the Description bit does work, just not the displayname.
Thursday, May 1, 2008 9:15 AM -
User1641955678 posted
If you set the DisplayName on a field, that string should show up as the header of that column in the gridview (among other places). Maybe you are looking for the string somehere else?
David
- Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Thursday, May 1, 2008 10:55 AM -
User-222364991 posted
Thanks
I have solved my problem with a combination of changing MetadataClass to MetadataType and adding the following references to my partial class
using
System.ComponentModel.DataAnnotations;using System.ComponentModel.Design;
so my code now looks like this
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Data.Linq; using System.Data.Linq.Mapping; using System.Linq; using System.Linq.Expressions; using System.Reflection; using System.Web.DynamicData; using System.ComponentModel.DataAnnotations; using System.ComponentModel.Design; [MetadataType(typeof(Asset_Metadata))] partial class Asset { } public class Asset_Metadata { [DisplayName("Title")] public string AssetTitle { get; set; } [DisplayName("Subject")] public string AssetSubject { get; set; } }
I was at the MSDN roadshow in Manchester yesterday and the Dynamic Data stuff was great and almost as cool as Hard rock memorabilia and silverlight airline demos[:D] and much more useful[:D][:D][:D]
Friday, May 2, 2008 5:15 AM -
User1030110584 posted
I have a similar problem. For a test project, I have a database with two tables. I created a file called Metadata.cs in the App_Code folder of my project. In that file I have the following content, the intent of which is to prevent the inclusion of the Resources table and to change the names displayed for the Resource_Types table and its columns:
1 using System; 2 using System.Collections.Generic; 3 using System.ComponentModel; 4 using System.ComponentModel.DataAnnotations; 5 using System.Linq; 6 using System.Web; 7 using System.Web.DynamicData; 8 9 /// <summary> 10 /// Contains the metadata for Dynamic Data to use when displaying the data 11 /// </summary> 12 13 14 /// Exclude the Resources table from the model 15 [ScaffoldTable(false)] 16 public partial class Resources 17 { 18 } 19 20 // Attach the Resource_TypesMetadata to the Resource_Types class 21 [MetadataType(typeof(Resource_TypesMetadata))] 22 public partial class Resource_Types 23 { 24 } 25 26 [DisplayName("Resource Types")] 27 public class Resource_TypesMetadata 28 { 29 // Override the display name 30 [DisplayName("Code")] 31 public object resource_type_code { get; set; } 32 // Override the display name 33 [DisplayName("Description")] 34 public object resource_type_description { get; set; } 35 }
The problem I'm having is that although the Resources table is excluded, the names for the Resource_Types table and columns are not changed.
Any idea what I'm doing wrong?
- Mark
Tuesday, November 11, 2008 5:12 PM -
User-1005219520 posted
see this post
Tuesday, November 11, 2008 6:39 PM -
User745141034 posted
How to change the display name of a field in run-time,please?
I hope change the display name of a field through programing,how to do,please?
Wednesday, November 12, 2008 8:41 PM -
User-330204900 posted
H Zzdfc, have a look at this method of changing column and table names at runtime: Dynamic Data – Custom Metadata Providers
hope this helps [:D]
Thursday, November 13, 2008 3:58 AM