Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 830
  • Last Modified:

how to set the default value in the dropdownlist control

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
learningnet
Asked:
learningnet
2 Solutions
 
BinuthCommented:
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
 
Daniel WilsonCommented:
<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
 
learningnetAuthor Commented:
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

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now