Thursday, December 08, 2005

Christmas Calendar 8/24

What to do when you want to bind more than one field into a dropdownlist?

You've created your object,

BEHOLD MY BEAUTIFUL OBJECT "MyObject" !!!

and you want a Dropdown to contain, say, both the Id property and the Name property.

"DOH" you might say, now I've got to create a new property that contains the textual representation of the Id and Name in the object.. "why oh why must my beautiful object depend on the whims of my presentation layer"

fear not ye brave coders. the solution is nigh!

write one of these beauts and you'll be set.

DropDownList1.DataTextField = MyObject.Id & ”-” & MyObject.Name;
DropDownList1.DataValueField = MyObject.Id;
DropDownList1.DataBind();

The & is not to be confused with && as in "logical AND". but is more like "AND concatenate THIS!! "

No comments: