[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

Append Blank Item to Databound ASP.net Dropdown

Posted on 2007-10-18
3
Medium Priority
?
2,453 Views
Last Modified: 2008-01-09
I am looking to add a blank default item to a dropdown list in an ASP.net form.
I can not add the item directly to the database in this situation.

I have tried the following code for the DropdownList however the item did not appear:

 <asp:DropDownList ID="C_DepartmentID" runat="server" AutoPostBack="True" DataSourceID="D_DEPTs"
                            DataTextField="Name" DataValueField="ID" Width="16em">
                                <asp:ListItem Value="0" Selected="true">Select Department</asp:ListItem>
                            </asp:DropDownList>

I Then set the AppendDataBoundItems="True"  
This does make the blank item appear in the list, however the side effect is that if the conditions populating the list change- the list items from the new parameters are appended to the old and both can be selected. This can obviously cause some big problems.

 Is there a fix for this side effect, or perhaps another method of adding this blank default ?
As always thank you for your help!


0
Comment
Question by:apocalypse910
3 Comments
 
LVL 4

Assisted Solution

by:S31B1
S31B1 earned 800 total points
ID: 20102283
In instances such as the one you present I use a UNION statement in my SQL to add an aditional SELECT statment with hard coded values that provide the column you need, for example
(
SELECT 'Select Department' as Name, 0 as Value
)
UNION
(
SELECT Name, Value FROM Table
)

I'm not sure if this would contravean your statment about not adding to the database though?
0
 
LVL 26

Accepted Solution

by:
Rejojohny earned 1200 total points
ID: 20103887
in page load

if not ispostback() then
  DepartmentID.Items.Insert(0, "Select department")
end if

Rejo
0
 

Author Comment

by:apocalypse910
ID: 20171255
I sincerely apologize for the delay in getting back to you. i got involved in another project and hadn't had a chance to work on this issue for a bit. Thank you very much for your time and the excellent solutions.
0

Featured Post

Hire Technology Freelancers with Gigs

Work with freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely, and get projects done right.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

One of the pain points with developing AJAX, JavaScript, JQuery, and other client-side behaviors is that JavaScript doesn’t allow for cross domain request for pulling content. For example, JavaScript code on www.johnchapman.name could not pull conte…
A quick way to get a menu to work on our website, is using the Menu control and assign it to a web.sitemap using SiteMapDataSource. Example of web.sitemap file: (CODE) Sample code to add to the page menu: (CODE) Running the application, we wi…
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an anti-spam), the admin…
Please read the paragraph below before following the instructions in the video — there are important caveats in the paragraph that I did not mention in the video. If your PaperPort 12 or PaperPort 14 is failing to start, or crashing, or hanging, …
Suggested Courses

872 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question