Re: DropDownLists and NULL values
- From: "sloan" <sloan@xxxxxxxxx>
- Date: Tue, 31 Jan 2006 02:17:07 -0500
I usually do a
ddlMyDDL.Items.insert(0 , new ListItem(0 , "--Select--"))
I'm going from memory, but what I mean is a new ListItem with the value of
0, text of "--Select--", and I use the Items.Insert (at position 0)
(which I do immediately AFTER the DataBind() method)
That may not be what you mean...as in..if the value coming from the DB is
null to begin with or something.
...
But what I propose will put that "fake" value in there.
If my database doesn't actually like a zero, in my businesslayer, I will
convert a 0 to DBNULL ...
...
Ok, I just did a quick google, and found this:
http://www.4guysfromrolla.com/webtech/073101-1.shtml
(They don't do the explicit ListItem.Value of 0 like I did, but its the same
concept)
"Neil" <neil.jellis@xxxxxxxxxxxxxxxxxxxxxxx> wrote in message
news:1138604752.204475.309960@xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
> I'd like to know what the best method is for handling NULL (or 0)
> values within a DropDownList.
>
> If a database lookup value is optional I would normally consider
> leaving it NULL but this leads to problems with DropDownLists because
> there is no value to bind to and an eror is raised.
>
> I considered a method whereby the DropDownList has a an index value of
> 0 inserted with a space character as the text after it has retrieved
> the data from the source table. As it has a 0 index value it would
> appear at the top of the list and would indicate that no selection had
> been made. Within the database the column would be defined with a
> default value of 0. Thus the 0 value retrieved from the db would bind
> to the 0 value inserted into the DropDownList.
>
> I've tried to get this approach to work but the code always errors
> because there is no value to bind the retrieved db value against. I've
> obviously not got the 0 value insertion code in the right place.
>
> The web form is filling a formview control via a sqldatasource
> following the selection of a record on a gridview.
>
>
> Can someone point me in the right direction as to how such optional
> lookup data in a dropdownlist should be handled
>
.
- References:
- DropDownLists and NULL values
- From: Neil
- DropDownLists and NULL values
- Prev by Date: Re: Conditional use of Eval in asp.net 2.0 with Gridview
- Next by Date: Re: html or web control question
- Previous by thread: Re: DropDownLists and NULL values
- Next by thread: Re: Help on DataBinding ??
- Index(es):
Relevant Pages
|