Reputation: 3065
Been googling for a while now and cant see this answer anywhere.
Using ASP.NET (VB) in VS2013.
I have a drop down list with 4 preset values (see below)
<asp:DropDownList ID="TimeTextMins" runat="server" style="font-family:'Century Gothic'; font-size:12pt;">
<asp:ListItem Value="00"></asp:ListItem>
<asp:ListItem Value="15"></asp:ListItem>
<asp:ListItem Value="30"></asp:ListItem>
<asp:ListItem Value="45"></asp:ListItem>
</asp:DropDownList>
The values represent minutes in 15 minute splits.
When the page loads I want to put the current time in minutes in this drop down, the user can then change to a 15 minute segment if required. I know how to get the current minute in VB nut no idea how to set the DDL to it. Using selectedvalue doesn't work and neither does .text.
Cheers
Upvotes: 0
Views: 354
Reputation: 795
You can do it using standard drop down and you can hide current time item when list is expanded, without postback. You have to write js function and assign it to onmousedown event and do a little trick.
First set your calculated current time as 'Text' property of first item on the list (leaving it's 'value' as '00') in code behind on Page_Load.
When page loads you will see current time as selected time in dropdown. Now in js function assigned to onmousedown you simply set innerText property of first option to '00': You need flag variable to do it only once:
var flag = 0;
function dropDownList_MouseDown()
{
if (flag == 0)
{
var list = document.getElementById('<%= DropDownList1.ClientID %>');
list.options[0].innerText = '00';
flag =1;
}
}
Upvotes: 0
Reputation: 8160
Since a dropdown can only be set to values that are in the list, you will need to dynamically add the current minute to the dropdown values in the page Load
event:
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
If Not Page.IsPostBack Then
Dim currentMinute = Date.Now.Minute
Dim currentMinuteString = CStr(currentMinute).PadLeft(2, "0"c)
If currentMinute Mod 15 = 0 Then
Me.TimeTextMins.SelectedValue = currentMinuteString
Else
Dim position = (currentMinute \ 15) + 1
Me.TimeTextMins.Items.Insert(position, currentMinuteString)
Me.TimeTextMins.SelectedValue = currentMinuteString
End If
End If
End Sub
Update
Based on your comment on the question that you only want the 15 minute intervals in the dropdown, I don't know that you can do that with the standard drop down list control, but there are 3rd party HTML controls that are more combobox like. What you could do is remove the added item when the selection is changed:
Private Sub TimeTextMins_SelectedIndexChanged(sender As Object, e As EventArgs) Handles TimeTextMins.SelectedIndexChanged
Dim itemsToRemove =
Me.TimeTextMins.Items.Cast(Of ListItem)() _
.Where(Function(x) x IsNot Me.TimeTextMins.SelectedItem AndAlso CInt(x.Value) Mod 15 <> 0) _
.ToList()
For Each item In itemsToRemove
Me.TimeTextMins.Items.Remove(item)
Next
End Sub
You will need to add AutoPostBack="true"
to your asp:DropDownList
tag.
Upvotes: 3