Archive for November 23rd, 2009
ASP.NET 4.0 ScriptManager Improvements
JQuery Validator plus Element IDs, and Names
OK, just discovered an interesting implementation detail of JQuery Validator (one that I should have known about already as well).
OK, if you have a form where the inputs of the for have both id and name attributes, the name attributes trump id.
So if you have this:
1: <input type='type' id='RegisterUserName' name='userName' />
Then you write the validation against ‘user’Name’, not ‘RegisterUserName’.
1: $("RegisterForm").validate({
2: rules: { userName: {required: true} }
3: });
If you do not you will get all sorts of strange null exception errors (which in JavaScript means something returned as ‘undefined’.
How this happens
People have been talking about the great flexibility of Asp.Net MVC for a while now, but there are a few things to be aware of. One of them is multiple forms.
Say you have a web page that has two forms on it, one to register users, the other to log users in. Both forms have ‘username’ and ‘password’ fields. Asp.Net MVC still cannot get you around the need to keep ids unique – but names can be reused. These names are also read by Asp.Net Action Controllers. So if you have multiple forms all submitting to the same Controller Action, as long as the name attributes are set it will hook up.
Anyway, this is a short post, mostly for myself, so I don’t spend two hours figuring this out again.
Silverlight 4 Beta – Using Silverlight as Drop Target
Have you ever wanted to give the user the ability to drag a file from you desktop or file explorer onto your Silverlight application? Well, up until now you couldn’t. What’s that sound? It’s a bird… it’s a plane… no, it’s Silverlight 4 Beta. Yes, Silverlight 4 Beta is coming to the rescue and giving you the ability to have your Silverlight application act as a drop target. And it is so easy to do. Observe.
First just identify the UI element you want to use as the drop target, then set the AllowDrop property to true. Then just handle the Drop events.
Here is a quick example. This examples allows a user to drag a bitmap from their file system into my application and displays the images in a ScrollViewer. Here is the XAML
<StackPanel x:Name="LayoutRoot" Background="White">
<TextBlock Text="Drop image below." FontSize="16" FontWeight="BOld" HorizontalAlignment="Center" />
<ScrollViewer x:Name="ImagesTarget" Background="White" Width="800" MinHeight="300"
VerticalScrollBarVisibility="Hidden" HorizontalScrollBarVisibility="Auto" AllowDrop="True">
<ItemsControl x:Name="ImageList">
<ItemsControl.ItemTemplate>
<DataTemplate>
<Image Source="{Binding}" Margin="5" Stretch="UniformToFill" Height="240" />
</DataTemplate>
</ItemsControl.ItemTemplate>
<ItemsControl.ItemsPanel>
<ItemsPanelTemplate>
<StackPanel Orientation="Horizontal" VerticalAlignment="Center" HorizontalAlignment="Center"/>
</ItemsPanelTemplate>
</ItemsControl.ItemsPanel>
</ItemsControl>
</ScrollViewer>
</StackPanel>
I create a ObservableCollection of bitmap images and use this as the ItemsSource for the ScrollViewer.
ObservableCollection<BitmapImage> _images = new ObservableCollection<BitmapImage>();
I hook into the Drop event in the load event of the form.
ImagesTarget.Drop += new DragEventHandler(ImagesTarget_Drop);
Now lets implement the Drop event to loop through all the files and add them to the list.
void ImagesTarget_Drop(object sender, DragEventArgs e)
{
if (e.Data == null)
return;
IDataObject dataObject = e.Data as IDataObject;
FileInfo[] files =
dataObject.GetData(DataFormats.FileDrop) as FileInfo[];
foreach (FileInfo file in files)
{
try
{
using (var stream = file.OpenRead())
{
var imageSource = new BitmapImage();
imageSource.SetSource(stream);
_images.Add(imageSource);
}
}
catch (Exception)
{
MessageBox.Show("Not a suppoted image.");
}
}
}
Bingo… Bango… Boom, that is all there is to it. And of course, you can handle the other drag events to have more control over what is being dropped and where.
Is Microsoft Taking Dynamic Languages Seriously?
Specifically IronPython and IronRuby.
Consider this …
- IronPython got underway in July of 2004. Five years later it appears IronPython is still not a candidate to be a first class language in the .NET framework and tools. You can vote on this issue.
- Microsoft first released IronRuby at Mix in 2007. Nearly three years later it appears IronRuby is still not a candidate to be a first class language in the .NET framework and tools. You can vote on this issue.
A first class language is deployed when the full .NET framework is installed. It’s as easy to find as csc.exe. It’s not a language you have to ask the IT department to install separately. It’s not a language that requires you to jump out of Visual Studio to edit or run.
Most of all, a first class language doesn’t require justification to higher powers. A first class language is pre-certified and stamped with a seal of approval. It’s as easy to use in the locked-down big corporate setting as the company paper shredder.
The DLR and the PDC
I was depressed when I read the session list from Microsoft’s recent Professional Developers Conference. If you browse the session list you’ll find hundreds of sessions covering cloud computing, Sharepoint, Silverlight, SQL Server, and modeling. There are a handful of sessions covering concurrency, and a few dedicated to C++.
There is exactly one session featuring the Dynamic Language Runtime in a significant fashion. The title is Using Dynamic Languages to Build Scriptable Applications. You can learn how to augment an existing application after you’ve done all the real work in a first class language.
Perhaps Microsoft is just hedging their bets, but it’s clear that that dynamic languages aren’t high on the priority list. Maybe they’ll be first class citizens one day, but many developers are tired of waiting.
P.S. Is Managed JScript dead?
Silverlight 4 Beta – Text Trimming
Before Silverlight 4 if you wanted to dynamically add ellipses (the commonly used … to show that the sentence continues), you have to invent your own implementation which usually involves creating your own control that wraps a text based control and then manipulate the text inside based on the size. Similar to what Robby Ingebretsen had to do here.
Well, that is no longer the case. Introducing Text Trimming.
<TextBlock FontSize="16"
TextTrimming="WordEllipsis"
Text="A man is known by the company he keeps."
Width="120"/>
Displays as: “A man is…”
This is a nice addition to the Silverlight framework.