如何将SQL Server时间值绑定到ASP.Net Web表单中的下拉列表?

时间:2023-01-14 16:33:07

I'm trying to figure out why this wont work. Any help or insight would be appreciated. I'm trying to bind to a Time column in the selected value for a drop down list:

我想弄清楚为什么这不起作用。任何帮助或见解将不胜感激。我正在尝试绑定到下拉列表的选定值中的时间列:

<asp:DropDownList ID="lblReadyTime" runat="server"
    DataTextFormatString="{0:htt}"
    SelectedValue='<%#Bind("ReadyTime", "{0:htt}")%>'>
    <asp:ListItem Text="12AM" Value="12AM" />
    <asp:ListItem Text="1AM" Value="1AM" />
    <asp:ListItem Text="2AM" Value="2AM" />
    <asp:ListItem Text="3AM" Value="3AM" />
    <asp:ListItem Text="4AM" Value="4AM" />
    <asp:ListItem Text="5AM" Value="5AM" />
    <asp:ListItem Text="6AM" Value="6AM" />
    <asp:ListItem Text="7AM" Value="7AM" />
    <asp:ListItem Text="8AM" Value="8AM" />
    <asp:ListItem Text="9AM" Value="9AM" />
    <asp:ListItem Text="10AM" Value="10AM" />
    <asp:ListItem Text="11AM" Value="11AM" />
    <asp:ListItem Text="12PM" Value="12PM" />
    <asp:ListItem Text="1PM" Value="1PM" />
    <asp:ListItem Text="2PM" Value="2PM" />
    <asp:ListItem Text="3PM" Value="3PM" />
    <asp:ListItem Text="4PM" Value="4PM" />
    <asp:ListItem Text="5PM" Value="5PM" />
    <asp:ListItem Text="6PM" Value="6PM" />
    <asp:ListItem Text="7PM" Value="7PM" />
    <asp:ListItem Text="8PM" Value="8PM" />
    <asp:ListItem Text="9PM" Value="9PM" />
    <asp:ListItem Text="10PM" Value="10PM" />
    <asp:ListItem Text="11PM" Value="11PM" />
</asp:DropDownList>

2 个解决方案

#1


0  

You need to convert to a DateTime first and then get the correct time.

您需要先转换为DateTime,然后获取正确的时间。

SelectedValue='<%# Convert.ToDateTime(Eval("ReadyTime")).ToString("htt", System.Globalization.CultureInfo.InvariantCulture) %>'

#2


0  

I fixed it by making the values match the Time literals I wanted and getting rid of the formatting references.

我通过使值与我想要的时间文字匹配并删除格式引用来修复它。

<asp:DropDownList ID="lblReadyTime" runat="server"
    SelectedValue='<%#Bind("ReadyTime")%>'>
    <asp:ListItem Text="12AM" Value="00:00:00" />
    <asp:ListItem Text="1AM" Value="01:00:00" />
    <asp:ListItem Text="2AM" Value="02:00:00" />
    <asp:ListItem Text="3AM" Value="03:00:00" />
    <asp:ListItem Text="4AM" Value="04:00:00" />
    <asp:ListItem Text="5AM" Value="05:00:00" />
    <asp:ListItem Text="6AM" Value="06:00:00" />
    <asp:ListItem Text="7AM" Value="07:00:00" />
    <asp:ListItem Text="8AM" Value="08:00:00" />
    <asp:ListItem Text="9AM" Value="09:00:00" />
    <asp:ListItem Text="10AM" Value="10:00:00" />
    <asp:ListItem Text="11AM" Value="11:00:00" />
    <asp:ListItem Text="12PM" Value="12:00:00" />
    <asp:ListItem Text="1PM" Value="13:00:00" />
    <asp:ListItem Text="2PM" Value="14:00:00" />
    <asp:ListItem Text="3PM" Value="15:00:00" />
    <asp:ListItem Text="4PM" Value="16:00:00" />
    <asp:ListItem Text="5PM" Value="17:00:00" />
    <asp:ListItem Text="6PM" Value="18:00:00" />
    <asp:ListItem Text="7PM" Value="19:00:00" />
    <asp:ListItem Text="8PM" Value="20:00:00" />
    <asp:ListItem Text="9PM" Value="21:00:00" />
    <asp:ListItem Text="10PM" Value="22:00:00" />
    <asp:ListItem Text="11PM" Value="23:00:00" />
</asp:DropDownList>

#1


0  

You need to convert to a DateTime first and then get the correct time.

您需要先转换为DateTime,然后获取正确的时间。

SelectedValue='<%# Convert.ToDateTime(Eval("ReadyTime")).ToString("htt", System.Globalization.CultureInfo.InvariantCulture) %>'

#2


0  

I fixed it by making the values match the Time literals I wanted and getting rid of the formatting references.

我通过使值与我想要的时间文字匹配并删除格式引用来修复它。

<asp:DropDownList ID="lblReadyTime" runat="server"
    SelectedValue='<%#Bind("ReadyTime")%>'>
    <asp:ListItem Text="12AM" Value="00:00:00" />
    <asp:ListItem Text="1AM" Value="01:00:00" />
    <asp:ListItem Text="2AM" Value="02:00:00" />
    <asp:ListItem Text="3AM" Value="03:00:00" />
    <asp:ListItem Text="4AM" Value="04:00:00" />
    <asp:ListItem Text="5AM" Value="05:00:00" />
    <asp:ListItem Text="6AM" Value="06:00:00" />
    <asp:ListItem Text="7AM" Value="07:00:00" />
    <asp:ListItem Text="8AM" Value="08:00:00" />
    <asp:ListItem Text="9AM" Value="09:00:00" />
    <asp:ListItem Text="10AM" Value="10:00:00" />
    <asp:ListItem Text="11AM" Value="11:00:00" />
    <asp:ListItem Text="12PM" Value="12:00:00" />
    <asp:ListItem Text="1PM" Value="13:00:00" />
    <asp:ListItem Text="2PM" Value="14:00:00" />
    <asp:ListItem Text="3PM" Value="15:00:00" />
    <asp:ListItem Text="4PM" Value="16:00:00" />
    <asp:ListItem Text="5PM" Value="17:00:00" />
    <asp:ListItem Text="6PM" Value="18:00:00" />
    <asp:ListItem Text="7PM" Value="19:00:00" />
    <asp:ListItem Text="8PM" Value="20:00:00" />
    <asp:ListItem Text="9PM" Value="21:00:00" />
    <asp:ListItem Text="10PM" Value="22:00:00" />
    <asp:ListItem Text="11PM" Value="23:00:00" />
</asp:DropDownList>