I’m newbie to Visual Studio 2010 .NET 4.0 Dynamic Data. Now I’m trying to do a simple dropdown list for my Web site (written in C#).
I’ve got a field called protocol (as in TCP-protocol). I want the user to be able to choose one of static values.
In olden days with classic ASP and HTML I would have used something like this:
<form name=form1 method="POST" action="./insert.asp">
...
<select name="PROTOCOL" size="1">
<option value="https" selected>https
<option value="sftp">sftp
<option value="ftps">ftps
</SELECT>
...
</form>
… and then handled the database in the ./insert.asp.
So far I’ve found instruction on how to do this in Visual Studio 2008 (.NET 3.5), but this doesn’t work in WS2010 and .NET 4.0:
http://csharpbits.notaclue.net/2008/07/dynamic-data-and-field-templates-your.html
Following the instructions in http://www.asp.net/web-forms/videos/aspnet-dynamic-data I’ve figured that in order to customize my fields I need to
- Create file for my custom code in the App_Code folder (MyService.cs).
- Create a ‘public partial class’ for my MS-SQL -table (service).
Here’s my (¿pathetic?) effort (App_Code/MyService.cs):
// MyService.cs
using System;
using System.ComponentModel.DataAnnotations;
using System.Web;
using System.ComponentModel;
[MetadataType(typeof(serviceMetadata))]
public partial class service
{
}
public class serviceMetadata
{
[EnumDataType(typeof(ProtocolType))]
public object protocol { get; set; }
}
public enum ProtocolType
{
https,
sftp,
ftps
}
This builds ok, but running it ends with “ArgumentException was unhandled by user code / The value passed in must be an enum base or an underlying type for an enum, such as an Int32.” message in \DynamicData\FieldTemplates\Enumeration.ascx.cs -file.
Help appreciated.
OK, since nobody has tried to answer this, I’ll try to answer my own question.
Seems to me that there is not out-of-the-box method for this problem. However, in my mind (please do correct me if I’m wrong) there are three possible solutions for this problem:
Change the service.protocol -field to numeric (int)
Change the enum -code:
More on the subject: http://msdn.microsoft.com/en-us/library/system.componentmodel.dataannotations.enumdatatypeattribute.aspx
Create your own FieldTemplate
Comments are welcome.