Solved

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

Posted on 2016-10-25
5
67 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
5 Comments
 
LVL 51

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 51

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 51

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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
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…

726 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