简体   繁体   中英

Displaying hours and minutes from a SQL Server time column in an ASP.Net DetailsView

In an ASP.Net DetailsView is a templete field called LateTimeArrivedAtSchool. The data for this field is from a SQL Server Time column. The data in the database in stored as hours, minutes and seconds.

We would like to display it as only hours and minutes.

Here is the markup for the template field:

<asp:TemplateField HeaderText="Late Time Arrived At School:" SortExpression="LateTimeArrivedAtSchool">
    <EditItemTemplate>
        <asp:TextBox ID="TextBoxLateTimeArrivedAtSchool" runat="server" 
            Text='<%# Bind("LateTimeArrivedAtSchool") %>'></asp:TextBox>
    </EditItemTemplate>

    <InsertItemTemplate>
        <asp:TextBox ID="TextBoxLateTimeArrivedAtSchool" runat="server" 
            Text='<%# Bind("LateTimeArrivedAtSchool") %>'></asp:TextBox>
    </InsertItemTemplate>

    <ItemTemplate>
        <asp:Label ID="LabelLateTimeArrivedAtSchool" runat="server" 
            Text='<%# Bind("LateTimeArrivedAtSchool", "{0:hh:mm}") %>'></asp:Label>
    </ItemTemplate>

    <ItemStyle ForeColor="Blue" />
</asp:TemplateField>

using "{0:hh:mm}" produces an error that states the "Input string was not in a correct format."

Can you tell me what we need to use in the Bind statement?

* UPDATE * I changed the SQL Server data type of the column from a Time column to a DateTime column and was able to display the time only portion of the data.

使用eval代替bind

Text='<%# string.Format("{0:hh:mm}", Eval("LateTimeArrivedAtSchool")) %>'

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM