Wednesday 3 February 2010

Asp.Net Dynamic Data – Default Value

Ok perhaps I was wrong in my last post, Display Name may have to vie for the title of ‘simplest hint’ with Default Value.

This attribute allows you to specify the default value of a field when performing an insert of a new record. To use it simply decorate the property in the meta data class with the attribute and the value you wish to use:

[DefaultValue("UK")]

public object Country { get; set;}


When entering a new record the screen now populates the country field with UK:



2010-02-03_2045

5 comments:

  1. How can I use the DefaultValue attributes to put the windowsidentity instead of a constant value like "user" for example [DefaultValue(HttpContext.Current.Request.ServerVariables["LOGON_NAME].ToString()]?

    ReplyDelete
  2. @Francisco unfortunately you can't. The value has to be set at run time but there is a way round this.

    If you can create your own attribute you can easily make it provide whatever value you want to.

    If you only need it once on the page, which with logon name is likley, you could simply create a custom template and write the value directly in the markup.

    Hope this helps.

    ReplyDelete
  3. Ok Nathan, thank you for your idea, now I have a better notion of that, but I want to put these value in a textbox control in the markup. How can I do that?

    Thanks in advance.

    ReplyDelete
  4. @Francisco you could do it one of two ways.

    If you are going to want to reuse this in other projects and all over the current project look into an attribute.

    If you want it for just one specific thing you could just create a custom field template, which is dead easy, then just use the UIHint attribute to get DD to use that for your field.

    ReplyDelete