[Last Call] Learn how to a build a cloud-first strategyRegister Now

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

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

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
karthik80c
Asked:
karthik80c
  • 3
  • 2
1 Solution
 
Ryan ChongCommented:
something like this?
ddlTime.ClearSelection();
            if (ddlTime.Items.FindByText(datewithtime[1]) != null)
            {
                ddlTime.Items.FindByText(datewithtime[1]).Selected = true;
            }

Open in new window

0
 
karthik80cAuthor Commented:
Hi Alpha,

I tried with your code . its not getting into the if condition
0
 
Ryan ChongCommented:
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
 
karthik80cAuthor Commented:
Hi Ryan

The value is
08:30:00
0
 
Ryan ChongCommented:
there are:

08:30 AM
08:30 PM


in your dropdownlist but not 08:30:00

you need an exact match
1

Featured Post

How to Use the Help Bell

Need to boost the visibility of your question for solutions? Use the Experts Exchange Help Bell to confirm priority levels and contact subject-matter experts for question attention.  Check out this how-to article for more information.

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