2013-01-03 28 views
1

在ASP.Net DetailsView是一個名爲LateTimeArrivedAtSchool的模板字段。該字段的數據來自SQL Server時間列。數據庫中的數據以小時,分鐘和秒的形式存儲。在ASP.Net中顯示SQL Server時間列的小時和分鐘DetailsView

我們希望將它顯示爲只有幾小時和幾分鐘。

下面是模板字段標記:

<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> 

使用「{0:HH:MM}」產生,指出錯誤「輸入字符串的不正確的格式。」

你能告訴我我們需要在綁定語句中使用嗎?

* UPDATE * 我從Time列到DateTime列改變的列的SQL Server數據類型,並能夠顯示時間只有數據的部分。

+0

你可以看看這個[問題](http://stackoverflow.com/questions/275194/formatting-databinder-eval-data)。它顯示瞭如何在aspxControls中使用Binding格式 –

回答

1

使用eval而不是綁定

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

感謝大家的回覆。我會嘗試Sundeep的,並看看上面的評論中的鏈接,並讓你知道它是如何工作的。 –

+0

Trying Eval給出了同樣的錯誤,所以我會嘗試查看是否可以從Pilgerstorfer Franz發佈的鏈接中學習如何執行此操作。 –

+0

鏈接建議我使用DataBinder.Eval,但也給出了相同的錯誤。我現在要嘗試更改Time to DateTime中列的SQL Server數據類型,並查看是否有所作爲。 –

相關問題