2010-03-22 83 views
6

我的ListView具有以下EditItemTemplate:它如何格式化數據綁定文本框中的文本?

<EditItemTemplate> 
    <tr style=""> 
     <td> 
      <asp:LinkButton ID="UpdateButton" runat="server" CommandName="Update" Text="Update" /> 
      <asp:LinkButton ID="CancelButton" runat="server" CommandName="Cancel" Text="Cancel" /> 
     </td> 
     <td> 
      <asp:TextBox ID="FundingSource1TextBox" runat="server" Text='<%# Bind("FundingSource1") %>' /> 
     </td> 
     <td> 
      <asp:TextBox ID="CashTextBox" runat="server" Text='<%# Bind("Cash") %>' /> 
     </td> 
     <td> 
      <asp:TextBox ID="InKindTextBox" runat="server" Text='<%# Bind("InKind") %>' /> 
     </td> 
     <td> 
      <asp:TextBox ID="StatusTextBox" runat="server" Text='<%# Bind("Status") %>' /> 
     </td> 
     <td> 
      <asp:TextBox ID="ExpectedAwardDateTextBox" runat="server" Text='<%# Bind("ExpectedAwardDate","{0:MM/dd/yyyy}) %>' onclientclick="datepicker()" /> 
     </td> 
    </tr> 
</EditItemTemplate> 

我想格式化ExpectedAwardDateTextBox所以它顯示了一個短日期時間,但還沒有找到一個辦法做到這一點沒有進入後面的代碼。在項目模板,我有以下行來格式化出現在拉布勒日期:

<asp:Label ID="ExpectedAwardDateLabel" runat="server" Text='<%# String.Format("{0:M/d/yyyy}",Eval("ExpectedAwardDate")) %>' /> 

而且我想找到一個類似的方法做的insertItemTemplate

回答

6

可以使用Bind()超載這樣的:

<%# Bind("ExpectedAwardDate", "{0:M/d/yyyy}") %> 

同樣爲您評估和演示過:

<asp:Label ID="ExpectedAwardDateLabel" runat="server" 
      Text='<%# Eval("ExpectedAwardDate","{0:M/d/yyyy}") %>' /> 
+0

完美!謝謝! – 2010-03-22 19:03:28

+0

@Abe - 歡迎:) – 2010-03-22 19:03:49

1

如果您需要做更復雜的格式,然後更改日期的顯示,你也可以使用OnItemDataBound

protected void ContactsListView_ItemDataBound(object sender, ListViewItemEventArgs e) 
{ 
    if (e.Item.ItemType == ListViewItemType.DataItem) 
    { 
     // Display the e-mail address in italics. 
     Label EmailAddressLabel = (Label)e.Item.FindControl("EmailAddressLabel"); 
     EmailAddressLabel.Font.Italic = true; 
    } 
}