Viewing entries in
Development

Comment

Dynamic Data - Validation

ASP.NET Dynamic Data is fantastic in that it automatically picks up simple validation straight from your DBML/EF model.

It will pick up things like required fields, data types and maximum lengths. You can also manually assign validation rules to your classes using metadata and validation attributes.

The following is a list of available validators from the System.ComponentModel.DataAnnotations namespace.

You use these by attribute by creating a metadata class and associating it with your domain class using the MetadataType attribute

[MetadataType(typeof(CustomerMetadata))]
public partial class Customer { }

public class CustomerMetadata
{
	[Required(ErrorMessage = "First Name is a required field")]
	[StringLength(30, ErrorMessage="The customer's first name cannot be more than 30 characters")]
	public object FirstName { get; set; }

	[RegularExpression(@"\b[A-Z0-9._%-]+@[A-Z0-9.-]+\.[A-Z]{2,4}\b")]
	public object EmailAddress { get; set; }

	[Range(18,150, ErrorMessage = "Customer must be 18 to 150 years old")]
	public object Age { get; set; }
}

Note: Make sure the partial class is in the same namespace as your DBML/EF classes.

Now you get extra validation with a few lines of code.

Comment

6 Comments

Cross browser copy and paste - with jQuery.Copy

See my updated post Cross Browser Copy and Paste - with ZeroClipboard *updated* for a different plugin to avoid security issues with Flash 10

 

Thank god for jQuery and addons

The Scenario

I was working on a client project for SSW when the client reported a bug in the web app.

The bug involved a dynamically generated mailto link that got updated when you selected multiple employees. The client was reporting an error when he selected more than 10 employees to email. His Lotus Notes mail client popped up an error saying:

Error processing command line arguments

Testing this myself I found that Outlook 2007 could easily support the emails of 30-40 employees before the mailto link stopped working.

The Cause

It turns out that the mailto spec has a limit and the mail clients also have a limit. Lotus Notes only handles 240 characters in the mailto link and other modern mail clients like Outlook 2007 support the 2083 characters - the max length of a URL

This explains the discrepancy in testing.

The fix - JQuery to the rescue

Since this is a limitation of the HTML spec we needed another solution to meet the client’s requirement of “I want to be able to select multiple employees and send an email to all of them”

We could have created an email form that used SMTP to send out the email - but the client wanted to use Lotus Notes as his mail client.

We ended up changing the “email” button to copy all the emails (comma separated) onto the clipboard and popped open a new email window. All the client had to do was hit CTRL + V and paste the emails into the TO field. This was the quickest and most cost effective solution that gave the client the flexibility to use their own email client.

There is a JQuery plugin called jquery.copy that provided cross browser copy and paste by using a flash (swf) file. This is similar to how the syntax highlighter on my blog works.

Once you reference the jquery.copy.js file all you need to do to push data into the clipboard is run the following:

$.copy("some text to copy");

Nice and easy ;)

Note: you may need to change the path the the SWF file in jquery.copy.js to get this to work

6 Comments

2 Comments

ASP.NET Dynamic Data - GUIDs

Dynamic Data by default doesn’t play well with GUID primary keys, or more correctly LINQ to SQL doesn’t auto generate the GUIDs for you by default. You will get validation errors when you try to insert

Note: the Entity Framework also suffers from this problem

You have two options here:

Option 1 - Set the Auto Generate Property to True

  1. In the DBML Designer, select the GUID primary key and select Properties
  2. Set the Auto Generated Value property to True
  3. Now the UserID field is hidden in the insert page and we are able to successfully insert

The problem with this method is that every time the DBML file gets regenerated you lose this change and you get the validation error again. For a more permanent solution you can try option 2

Option 2 - Turn Scaffolding off through metadata

  1. Create a metadata class and add the ScaffoldColumn(false) attribute to the UserID field
	using System.ComponentModel.DataAnnotations;

	[MetadataType(typeof(UserMetadata)]
	public partial class User { }

	public partial class UserMetadata
	{
		[ScaffoldColumn(false)]
		public object UserID { get; set; }
	}

This gives you a more permanent solution that can survive regenerating your DBML

2 Comments

2 Comments

Dynamic Data - Enable Inline Editing

I had a question from last night’s user group after the presentation regarding supporting inline editing. You can roll your own solution by creating your own page template with support for inline editing or you can change the routing and use the one built in with Dynamic Data.

To do this, in the Global.asax file, just comment out the default route and uncomment the second one that routes everything to the ListDetails template.


            // The following statement supports separate-page mode, where the List, Detail, Insert, and 
            // Update tasks are performed by using separate pages. To enable this mode, uncomment the following 
            // route definition, and comment out the route definitions in the combined-page mode section that follows.
            //routes.Add(new DynamicDataRoute("{table}/{action}.aspx")
            //{
            //    Constraints = new RouteValueDictionary(new { action = "List|Details|Edit|Insert" }),
            //    Model = model
            //});

            // The following statements support combined-page mode, where the List, Detail, Insert, and
            // Update tasks are performed by using the same page. To enable this mode, uncomment the
            // following routes and comment out the route definition in the separate-page mode section above.
            routes.Add(new DynamicDataRoute("{table}/ListDetails.aspx") {
                Action = PageAction.List,
                ViewName = "ListDetails",
                Model = model
            });

            routes.Add(new DynamicDataRoute("{table}/ListDetails.aspx") {
                Action = PageAction.Details,
                ViewName = "ListDetails",
                Model = model
            });

P.S. PPT and Video will be up shortly

2 Comments

46 Comments

Reserved.ReportViewerWebControl.axd not found

Ran into this issue the other day at work. We were developing some reporting services reports for a client and viewing them through a report viewer control in the intranet site we were developing.

The reports ran fine in the development environment but as soon as it was published to testing the reports behaved strangely. The test box was a Windows 2008 Server Standard Edition with IIS installed and .NET 3.5 SP1.

The reports page was reporting a 404 on the following resource “Reserved.ReportViewerWebControl.axd”

46 Comments