Solved

How do i select a value on dropdown list on the fly in asp.net

Posted on 2016-10-25
5
62 Views
Last Modified: 2016-10-26
Hi Experts ,

Basically am fetching a value from DB and try that value to get selected on dropdown menu on the fly . Here is my Dropdown list

<asp:DropDownList ID="ddlTime" Height="22px" runat="server">
                                    <asp:ListItem Text="12:00 AM" Value="12:00 AM"></asp:ListItem>
                                    <asp:ListItem Text="12:30 AM" Value="12:30 AM"></asp:ListItem>
                                    <asp:ListItem Text="01:00 AM" Value="01:00 AM"></asp:ListItem>
                                    <asp:ListItem Text="01:30 AM" Value="01:30 AM"></asp:ListItem>
                                    <asp:ListItem Text="02:00 AM" Value="02:00 AM"></asp:ListItem>
                                    <asp:ListItem Text="02:30 AM" Value="02:30 AM"></asp:ListItem>
                                    <asp:ListItem Text="03:00 AM" Value="03:00 AM"></asp:ListItem>
                                    <asp:ListItem Text="03:30 AM" Value="03:30 AM"></asp:ListItem>
                                    <asp:ListItem Text="04:00 AM" Value="04:00 AM"></asp:ListItem>
                                    <asp:ListItem Text="04:30 AM" Value="04:30 AM"></asp:ListItem>
                                    <asp:ListItem Text="05:00 AM" Value="05:00 AM"></asp:ListItem>
                                    <asp:ListItem Text="05:30 AM" Value="05:30 AM"></asp:ListItem>
                                    <asp:ListItem Text="06:00 AM" Value="06:00 AM"></asp:ListItem>
                                    <asp:ListItem Text="06:30 AM" Value="06:30 AM"></asp:ListItem>
                                    <asp:ListItem Text="07:00 AM" Value="07:00 AM"></asp:ListItem>
                                    <asp:ListItem Text="07:30 AM" Value="07:30 AM"></asp:ListItem>
                                    <asp:ListItem Text="08:00 AM" Value="08:00 AM"></asp:ListItem>
                                    <asp:ListItem Text="08:30 AM" Value="08:30 AM"></asp:ListItem>
                                    <asp:ListItem Text="09:00 AM" Value="09:00 AM"></asp:ListItem>
                                    <asp:ListItem Text="09:30 AM" Value="09:30 AM"></asp:ListItem>
                                    <asp:ListItem Text="10:00 AM" Value="10:00 AM"></asp:ListItem>
                                    <asp:ListItem Text="10:30 AM" Value="10:30 AM"></asp:ListItem>
                                    <asp:ListItem Text="11:00 AM" Value="11:00 AM"></asp:ListItem>
                                    <asp:ListItem Text="11:30 AM" Value="11:30 AM"></asp:ListItem>
                                    <asp:ListItem Text="12:00 PM" Value="12:00 PM"></asp:ListItem>

                                    <asp:ListItem Text="12:30 PM" Value="12:30 PM"></asp:ListItem>
                                    <asp:ListItem Text="01:00 PM" Value="01:00 PM"></asp:ListItem>
                                    <asp:ListItem Text="01:30 PM" Value="01:30 PM"></asp:ListItem>
                                    <asp:ListItem Text="02:00 PM" Value="02:00 PM"></asp:ListItem>
                                    <asp:ListItem Text="02:30 PM" Value="02:30 PM"></asp:ListItem>
                                    <asp:ListItem Text="03:00 PM" Value="03:00 PM"></asp:ListItem>
                                    <asp:ListItem Text="03:30 PM" Value="03:30 PM"></asp:ListItem>
                                    <asp:ListItem Text="04:00 PM" Value="04:00 PM"></asp:ListItem>
                                    <asp:ListItem Text="04:30 PM" Value="04:30 PM"></asp:ListItem>
                                    <asp:ListItem Text="05:00 PM" Value="05:00 PM"></asp:ListItem>
                                    <asp:ListItem Text="05:30 PM" Value="05:30 PM"></asp:ListItem>
                                    <asp:ListItem Text="06:00 PM" Value="06:00 PM"></asp:ListItem>
                                    <asp:ListItem Text="06:30 PM" Value="06:30 PM"></asp:ListItem>
                                    <asp:ListItem Text="07:00 PM" Value="07:00 PM"></asp:ListItem>
                                    <asp:ListItem Text="07:30 PM" Value="07:30 PM"></asp:ListItem>
                                    <asp:ListItem Text="08:00 PM" Value="08:00 PM"></asp:ListItem>
                                    <asp:ListItem Text="08:30 PM" Value="08:30 PM"></asp:ListItem>
                                    <asp:ListItem Text="09:00 PM" Value="09:00 PM"></asp:ListItem>
                                    <asp:ListItem Text="09:30 PM" Value="09:30 PM"></asp:ListItem>
                                    <asp:ListItem Text="10:00 PM" Value="10:00 PM"></asp:ListItem>
                                    <asp:ListItem Text="10:30 PM" Value="10:30 PM"></asp:ListItem>
                                    <asp:ListItem Text="11:00 PM" Value="11:00 PM"></asp:ListItem>
                                    <asp:ListItem Text="11:30 PM" Value="11:30 PM"></asp:ListItem>
                                </asp:DropDownList>

Open in new window



c# code i tried
 ddlTime.ClearSelection();
 ddlTime.SelectedIndex = ddlTime.Items.IndexOf(ddlTime.Items.FindByText(datewithtime[1]));

Open in new window

But the first value is shown always . Is there any other method am missing
0
Comment
Question by:karthik80c
  • 3
  • 2
5 Comments
 
LVL 50

Expert Comment

by:Ryan Chong
ID: 41858133
something like this?
ddlTime.ClearSelection();
            if (ddlTime.Items.FindByText(datewithtime[1]) != null)
            {
                ddlTime.Items.FindByText(datewithtime[1]).Selected = true;
            }

Open in new window

0
 

Author Comment

by:karthik80c
ID: 41858152
Hi Alpha,

I tried with your code . its not getting into the if condition
0
 
LVL 50

Expert Comment

by:Ryan Chong
ID: 41858158
can you debug and tell us what's the value of datewithtime[1] ?

make sure it matches the text of one of your dropdown items?
0
 

Author Comment

by:karthik80c
ID: 41858331
Hi Ryan

The value is
08:30:00
0
 
LVL 50

Accepted Solution

by:
Ryan Chong earned 500 total points
ID: 41858554
there are:

08:30 AM
08:30 PM


in your dropdownlist but not 08:30:00

you need an exact match
1

Featured Post

Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

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.

Question has a verified solution.

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

Suggested Solutions

Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…
Nobody understands Phishing better than an anti-spam company. That’s why we are providing Phishing Awareness Training to our customers. According to a report by Verizon, only 3% of targeted users report malicious emails to management. With compan…

791 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