Solved

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

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

Expert Comment

by:Ryan Chong
Comment Utility
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
Comment Utility
Hi Alpha,

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

Expert Comment

by:Ryan Chong
Comment Utility
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
Comment Utility
Hi Ryan

The value is
08:30:00
0
 
LVL 49

Accepted Solution

by:
Ryan Chong earned 500 total points
Comment Utility
there are:

08:30 AM
08:30 PM


in your dropdownlist but not 08:30:00

you need an exact match
1

Featured Post

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

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.
In this tutorial you'll learn about bandwidth monitoring with flows and packet sniffing with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're interested in additional methods for monitoring bandwidt…
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.

743 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

15 Experts available now in Live!

Get 1:1 Help Now