This is a simple example for how to provide an editing interface for date and time fields. 

Say that you are storing Booking details and your BookingDate field contains both the date and time, eg '21/05/2008 10:30:00 AM'

If you wanted to display the date and time strings seperately you can do this in a formview quite easily:

Date: 
<asp:Label ID="lblDate" runat="server" Text='<%# Eval("bookingdate", "{0:d}") %>' /> Time:
<asp:Label ID="lblTime" runat="server" Text='<%# Eval("bookingdate", "{0:%h:mm ttt}") %>' />

But to edit the date and time you would use a normal date field (with your choice of date picker) and then a dropdownlist for the time:

Date: 
<asp:TextBox ID="txtDate" runat="server" Text='<%# Eval("bookingdate", "{0:d}") %>' />
Time:
<asp:DropDownList ID="ddlHours" runat="server" Width="40px" Font-Size="11px">
<asp:ListItem>07</asp:ListItem>
    <asp:ListItem>08</asp:ListItem>
    <asp:ListItem>09</asp:ListItem>
    <asp:ListItem>10</asp:ListItem>
    <asp:ListItem>11</asp:ListItem>
    <asp:ListItem>12</asp:ListItem>
    <asp:ListItem>13</asp:ListItem>
    <asp:ListItem>14</asp:ListItem>
    <asp:ListItem>15</asp:ListItem>
    <asp:ListItem>16</asp:ListItem>
    <asp:ListItem>17</asp:ListItem>
    <asp:ListItem>18</asp:ListItem>
    <asp:ListItem>19</asp:ListItem>
    <asp:ListItem>20</asp:ListItem>
</asp:DropDownList>
<asp:DropDownList ID="ddlMins" runat="server" Width="40px" Font-Size="11px">
    <asp:ListItem>00</asp:ListItem>
    <asp:ListItem>05</asp:ListItem>
    <asp:ListItem>10</asp:ListItem>
    <asp:ListItem>15</asp:ListItem>
    <asp:ListItem>20</asp:ListItem>
    <asp:ListItem>25</asp:ListItem>
    <asp:ListItem>30</asp:ListItem>
    <asp:ListItem>35</asp:ListItem>
    <asp:ListItem>40</asp:ListItem>
    <asp:ListItem>45</asp:ListItem>
    <asp:ListItem>50</asp:ListItem>
    <asp:ListItem>55</asp:ListItem>
</asp:DropDownList>

Note the use of 'Eval' in the BookingDate Textbox - normally you would use 'Bind' for 2 way databinding, but we don't want to store the date - we want to store a combination of the date and time in the same field, which is done in the Formview's ItemUpdating subroutine:

Protected Sub Formview1_ItemUpdating(ByVal sender As Object, ByVal e & _
As System.Web.UI.WebControls.FormViewUpdateEventArgs) Handles Formview1.ItemUpdating
    e.NewValues("bookingdate") = CType(Formview1.FindControl("txtBookingDate"), TextBox).Text & _
        " " & CType(Formview1.FindControl("ddlHours"), DropDownList).SelectedValue & ":" & _
        CType(Formview1.FindControl("ddlMins"), DropDownList).SelectedValue
End Sub

This simply combines the textbox and dropdownlists into a formatted date which is then saved in your database as BookingDate.

Bartek Marnane

Digg It!DZone It!StumbleUponTechnoratiRedditDel.icio.usNewsVineFurlBlinkList

Comments

Comments are closed