Archive for October, 2006

Binding Databound controls to Enum

Posted in Microsoft.NET on October 10, 2006 by vasudevan

The other day I was curious to bind a static enum list to any databound control in .NET 2.0. The enum might be part of your business object in the middle tier. I did some research on this and found that is pretty easy and intuitive. Let’s see a example:


public enum ApartmentTypes
{
Studio,
OneBedroom = 1,
TwoBedroom = 2,
ThreeBedroom = 3
}

All user defined enums extend System.Enum class. So, to bind DropDownList control to ApartmentTypes enum, the code looks like this:


if (!Page.IsPostBack)
{
DropDownList1.DataSource = Enum.GetNames(typeof(ApartmentTypes));
DropDownList1.DataBind();
}

This code will bind to the enum directly and looking at the page source reveals that both the text and value for each entry in the Dropdownlist control is the same. Since no two entries will have the same entry, this shouldn’t cause any trouble. But, ideally the value should have the actual enum values and not the text to enable us to do more processing when doing a postback. To get the value for the entry selected in the dropdownlist, we should use the Parse method in the Enum class.


int value = (int)Enum.Parse(typeof(ApartmentTypes), DropDownList1.SelectedItem.Text);

This is the final code:

if (!Page.IsPostBack)
{
DropDownList1.DataSource = Enum.GetNames(typeof(ApartmentTypes));
DropDownList1.DataBind();
}
else
{
Response.Output.WriteLine((int)Enum.Parse(typeof(ApartmentTypes), DropDownList1.SelectedItem.Text));
}

Advertisements

Windows Media Player 6.4 advantages

Posted in Windows on October 2, 2006 by vasudevan

Windows Media Player 6.4 (albeit pretty old) still has big advantages over the new versions of WMPs. The major one is that it supports seeking a media file. Try the same media file on the new versions, seeking may not work. But, the WMP6.4 install program will not allow you to execute it on XP. It spits this error when  you try to run the installer:


This version of Windows Media Player is not currently supported on Microsoft Windows 2000. A version supporting Windows 2000 will be made available on www.microsoft.com at a future date.

To force WMP6.4 to run on XP, open the installer file “mpfull.exe” in WinRAR, extract all the files to a folder you want and run “mplayer2.exe”. It should open up fine. Go this link to get WMP6.4: WMP6.4

Also, another advantage with this version is smaller footprint when compared to the new versions.