Solved

how to set the default value in the dropdownlist control

Posted on 2008-10-09
3
802 Views
Last Modified: 2012-05-05
I am trying to show the list of names in the dynamic drop down listbox, using the following code.

 <asp:DropDownList ID="lstLearnItName" runat="server" DataSourceID="dsLearnItNames"
                                    DataTextField="LearnItName" DataValueField="LearnItID">
                                   
                                     
                        <asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server"
                         ErrorMessage="enter a Product ID" ControlToValidate="lstLearnItName"
                          SetFocusOnError="True" Display="Dynamic"></asp:RequiredFieldValidator>


<asp:SqlDataSource ID="dsLearnItNames" runat="server" ConnectionString="<%$ ConnectionStrings:sConnectionString %>"
                SelectCommand="SELECT * FROM [LearnItName]"></asp:SqlDataSource>


I just wanted to know how I can add a static listitem and set it to the default.

for an instance: <asp:ListItem Selected="true" Text="Please select" Value=""></asp:ListItem>

So that I can force the user to select an item before navigating to the next.

thanks in advance

regards
kay
0
Comment
Question by:learningnet
3 Comments
 
LVL 14

Accepted Solution

by:
Binuth earned 350 total points
Comment Utility
better to change your sql query than static listitem
like...

SELECT -1 AS LearnItID,"Please select" AS LearnItName UNION SELECT LearnItID,LearnItName FROM [LearnItName]
SELECT -1 AS LearnItID,"Please select" AS LearnItName UNION SELECT LearnItID,LearnItName FROM [LearnItName]

Open in new window

0
 
LVL 32

Assisted Solution

by:Daniel Wilson
Daniel Wilson earned 150 total points
Comment Utility
<asp:SqlDataSource ID="dsLearnItNames" runat="server" ConnectionString="<%$ ConnectionStrings:sConnectionString %>"
                SelectCommand="SELECT 0 as LearnItID, 'Please Select' as LearnItName UNION SELECT LearnItID, LearnItName FROM [LearnItName]"></asp:SqlDataSource>
0
 

Author Comment

by:learningnet
Comment Utility
guys,

its still not forcing me to select some other item from the drop down listbox

<asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server"
                         ErrorMessage="please choose the name" ControlToValidate="lstLearnItName"
                          SetFocusOnError="True" Display="Dynamic" InitialValue="Please Select"></asp:RequiredFieldValidator>

please can you still advise
thanks
0

Featured Post

What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

I recently went through the process of creating a Calendar Control of events with the basis of using a database to keep track of the dates that are selectable, one requirement was to have the selected date pop-up in a simple lightbox.  At first this…
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…
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…
You have products, that come in variants and want to set different prices for them? Watch this micro tutorial that describes how to configure prices for Magento super attributes. Assigning simple products to configurable: We assigned simple products…

744 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

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now