DataBinding web form to Guid property

DataBinding web form to Guid property

Old forum URL: forums.lhotka.net/forums/t/2084.aspx


SonOfPirate posted on Friday, January 05, 2007

Has anyone else experienced a problem databinding a dropdownlist control's SelectedValue property to a BO's Guid property when creating a new object?

I have a form that uses the FormView control to display and edit my BO.  One of the properties contains the Guid id value of an item in a lookup list.  I have a CslaDataSource control for both the object instance and another for the lookup list.  The DropDownControl appears as:

<asp:DropDownList ID="ManufacturingPlantDropDown" runat="server" DataSourceID="ManufacturingPlantsDataSource" DataTextField="Name" DataValueField="UniqueID" SelectedValue='<%# Bind("ManufacturingPlant") %>' Width="100%"></asp:DropDownList>

This is the case in all three view templates (Edit, Item & Insert) with the exception that the Eval statement is used in the ItemTemplate.

The ManufacturingPlant property in my BO defaults to System.Guid.Empty when a new instance is created; however, there is no record in the table with this identifier so I get an exception.

What is the best way to do this for properties that are optional and/or nullable?  I can't have it default to an existing value because that would require a lookup each time an object is created.  And, if the property's value is optional, then I wouldn't want it to possess a legitimate value unless the user has explicitly set it.

Any suggestions?

 

 

xal replied on Sunday, January 07, 2007

I usually add an item on top of the combobox with a "--Please select a value" text and an empty guid as value. If nulls are allowed, then i just change the text to "None" or something similar, but the approach is the same.
I also check for empty guids before calling the db in order to see if i should send the guid or a null.

Andrés

Copyright (c) Marimer LLC