2010-03-22 5 views
6

Je ListView qui a la EditItemTemplate suivante:Comment puis-je formater le texte dans une TextBox de databound?

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

Je voudrais formater le ExpectedAwardDateTextBox il montre un temps de date courte, mais ne l'ai pas trouvé un moyen de le faire sans entrer dans le code derrière. Dans le modèle de l'article que j'ai la ligne suivante pour formater la date qui apparaît dans le lable:

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

Et je voudrais trouver une méthode similaire à voir avec le insertItemTemplate.

Répondre

6

Vous pouvez utiliser la surcharge Bind() comme ceci:

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

même pour votre Eval aussi:

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

Parfait! Merci! –

+0

@Abe - Bienvenue :) –

1

Si vous devez faire un formatage plus complexe puis en changeant l'affichage d'une date, vous peut également utiliser 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; 
    } 
}