Der deutsche Education Blog

April, 2008

  • Maíra Wenzel's Blog

    The power of your feedback

    • 5 Comments

    Sometimes you're navigating in the MSDN Library and you spot an error in the documentation. You decide to send your feedback and you might wonder if someone reads your comments. Yes, we do! And we take them very seriously!

    From small typos to missing code samples to more serious errors, we analyze the comments and make the changes as possible.

    One example of a topic that was changed based on the user feedback was the reference for the GridView.RowEditing event. Some of the comments were asking for examples of how to use this event when you are setting the data source programatically and not using the DataSourceID property. So, in the new release of the documentation we added a new sample to the property with the comments in mind.

    You can compare the new version with the previous one!

    But, for us, it's also important that you give us positive feedbacks when you find useful topics. This helps us track when we're doing it right as well and use that as a model to be followed!

  • Maíra Wenzel's Blog

    ASP.NET Dynamic Data Attributes

    • 7 Comments

    Yesterday a new release of the ASP.NET Dynamic Data Preview was posted on Code Gallery.

    Dynamic Data uses a data model to interact with the underlying database. From the data model, Dynamic Data reads the metadata information to perform some built-in operations, such as, type validations or a required field validation. But you can extend the data model and add some extra metadata to a table or a particular field by using attributes.

    These attributes are in two namespaces: System.ComponentModel and System.ComponentModel.DataAnnotations.

    Here is the list of attributes you can use:

    Attribute name Description Category
    DisplayNameAttribute Specifies the header name for a column in the database. Display
    DescriptionAttribute Provides a textual message that will popup when the mouse pointer is moved over a field during editing (tooltip).  
    DefaultValueAttribute Provides a default value for a field when inserting a new value.  
    DataTypeAttribute

    Adds additional information about a data field, that is commonly treated as a plain string. Options include:

    • Custom
    • DateTime
    • Date
    • Time
    • Duration
    • PhoneNumber
    • Currency
    • Text
    • Html
    • MultilineText
    • EmailAddress
    • Password
    • Url
     
    DisplayColumnAttribute Specifies which column to display (in filters or in foreign key links). By default, Dynamic Data uses the first column of type string that it finds.  
    DisplayFormatAttribute Specifies a DataFormatString that can be used to format how numbers and dates are displayed. Display
    MetadataTypeAttribute Specifies the class that provides metadata for an entity class.  
    RangeAttribute Provides range based validation where the value of a field must be between a min and max value. Built-in field controls will render this using a RangeValidator control. Validation
    RegularExpressionAttribute Provides regular expression based validation where the value of a field must match a regular expression pattern. Built-in field controls will render the using a RegularExpressionValidator control. Validation
    RequiredAttribute Specifies that the field is required. Built-in field controls will render this using a RequiredFieldValidator control. Validation
    ScaffoldColumnAttribute Specifies if a data field is visible to the scaffolding mechanism. Display
    ScaffoldTableAttribute Specifies if a table is visible to the scaffolding mechanism. Display
    StringLengthAttribute Specifies the maximum length of a string. Validation
    UIHintAttribute Specifies a field template to use to render the particular field. Display /
    Behavior


    To apply an attribute to a table or a particular field, you cannot change the class generated by the O/R Designer or you will end up losing the changes if you update the data model.

    So the way you do this with Dynamic Data, is that you first create a partial class with name of your table. If the metadata refers to the table, you can apply the attribute in this partial class. For example:

    [DisplayColumn("FirstName")] 
    public partial class Employee { }


    But, when you want to add metadata to a particular field, you also need to create a special metadata class where you're going to apply the attribute to a particular field. And then, you're going to connect the partial entity class with this metadata class with the MetadataTypeAttribute, like in the following example:

    [MetadataType(typeof(ProductMetaData))] 
    public partial class Product { }
    
    public class ProductMetaData {
    
      [DisplayFormat(DataFormatString = "{0:C}")]
      public object UnitPrice { get; set; } 
    
    }
    

    I'll keep posting on Dynamic Data!

  • Maíra Wenzel's Blog

    Real control over markup

    • 0 Comments

    When you're working with data-bound controls and you want control over the generated markup, you end up using templated data-bound controls such as the FormView or ListView controls, or the TemplateField of a GridView control.

    But even when you're using these templated controls, sometimes you don't have real control over the generated markup. For example, if you have an Image control inside the ItemTemplate like the following code:

    <asp:Image runat="server"
      ID="ThumbnailImage"
      ImageUrl='<%# Eval("FileName", "images/{0}")>%'>
    </asp:Image>

    The generated markup will contain an additional style element that you haven't defined in your Web server control, like the following:

    <img id="PhotoListView_ctrl0_ThumbnailImage" 
      src="images/image1.jpg" style="border-width:0px;" />
    

    This happens because a Web control is not necessarily completely customizable. If you really need complete control over your final generated markup, i.e, you just want to show what you have defined, you can use HTML server controls. An HTML server control is an HTML element with a runat="server" attribute. In the last example, you could replace the Image control, with the <img> HTML element like the following:

    <img runat="server"
      id="ThumbnailImage"
      src='<%# Eval("FileName", "images/{0}")>%' />

    The end result is the same, however behind-the-scenes the generated markup does not contain anything extra, only what you have defined, as the following:

    <img id="PhotoListView_ctrl0_ThumbnailImage" 
      src="images/image1.jpg" />
    
Page 1 of 1 (3 items)

April, 2008