Solved

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

Posted on 2016-10-25
5
57 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

Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
SqlDataBase 7 48
How to close an asp.net web form using C# ? 4 31
Sort GridView by ID Descending 1 13
RegEx with optional part 4 24
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
Performance in games development is paramount: every microsecond counts to be able to do everything in less than 33ms (aiming at 16ms). C# foreach statement is one of the worst performance killers, and here I explain why.
Established in 1997, Technology Architects has become one of the most reputable technology solutions companies in the country. TA have been providing businesses with cost effective state-of-the-art solutions and unparalleled service that is designed…
Email security requires an ever evolving service that stays up to date with counter-evolving threats. The Email Laundry perform Research and Development to ensure their email security service evolves faster than cyber criminals. We apply our Threat…

810 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