Solved

how to set the default value in the dropdownlist control

Posted on 2008-10-09
3
810 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
ID: 22677532
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
ID: 22677533
<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
ID: 22678005
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

Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

Question has a verified solution.

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

Sometimes in DotNetNuke module development you want to swap controls within the same module definition.  In doing this DNN (somewhat annoyingly) swaps the Skin and Container definitions to the default admin selections.  To get around this you need t…
The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
This Micro Tutorial will give you a basic overview how to record your screen with Microsoft Expression Encoder. This program is still free and open for the public to download. This will be demonstrated using Microsoft Expression Encoder 4.
In a recent question (https://www.experts-exchange.com/questions/28997919/Pagination-in-Adobe-Acrobat.html) here at Experts Exchange, a member asked how to add page numbers to a PDF file using Adobe Acrobat XI Pro. This short video Micro Tutorial sh…

770 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