BizTalk Blog by Chris Han

System Design for Enterprise Agility,

  Home  |   Contact  |   Syndication    |   Login
  66 Posts | 9 Stories | 122 Comments | 79 Trackbacks

News

Article Categories

Archives

Post Categories

Image Galleries

BizTalk Bloggers

BizTalk on MSDN

Patterns & Architecture

SharePoint

In a master-detail type of page, you will alway be asked to add a item called “New Item” at the top of a master drop down list so that the user select this “New Item” to set the detail pane to “Insert” mode.

The traditional way is after you data bound the drop down list, you add a new ListItem to the drop down list. But not any more with Asp.NET 2.0 drop down list control!

Scott Gu has this post explains it in details: http://weblogs.asp.net/scottgu/archive/2006/01/29/436804.aspx

What I would like to add is when you add that “New Item” with Vs.NET 2005, by default, it add an item in this format:

<asp:ListItem>New Itemasp:ListItem>

This could cause trouble when “detail” data source use this control as the Data Key field because it doesn't has a value field. The work around is to manully add this item then, no big deal huh?: 

<asp:ListItem Text="New Item" Value="" /> 

Not so fast! Vs.NET 2005 has a small neat freak habit- it will remove the tag Value="" if its value is an empty string. So you have to make it something else like Value="-1" otherwise the “detail” data source will throw an exception: Input string was not in a correct format.

So what's the good looking for a developer like you:

<asp:ListItem Text="New Item" Value="-1" /> 

Oh, last thing: make sure you turn off the Viewstate for the drop down control. I can't tell you any more details about this. Otherwise there's no fun any more left for you. Try it yourself.

 

posted on Tuesday, April 25, 2006 9:15 AM

Feedback

# re: Sweet AppendDataBoundItems Property for Asp.NET 2.0 Drop Down Control 1/10/2008 11:40 AM newhanger
Why don't you just add it in the database side.
use

select 0,'--select name--'
Union ALL
select nameId,Name from name



# re: Sweet AppendDataBoundItems Property for Asp.NET 2.0 Drop Down Control 11/18/2008 12:52 AM Dhaval Upadhyay
But one problem what happen if you bind database sided and if you not select the dropdown list valueit it will alwase pass -1 in to database( and may foreigney reference also occur)

Post A Comment
Title:
Name:
Email:
Website:
Comment:
Verification: