views:

87

answers:

2

I have ListView that has the following 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>

I would like to format the "ExpectedAwardDateTextBox" so it shows a short date time but haven't found a way to do this without going into the code behind. In the Item template I have the following line to format the date that appears in the lable:

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

And I would like to find a similar method to do with the insertItemTemplate.

+2  A: 

You can use the Bind() overload like this:

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

Same for your Eval too:

<asp:Label ID="ExpectedAwardDateLabel" runat="server" 
           Text='<%# Eval("ExpectedAwardDate","{0:M/d/yyyy}") %>' />
Nick Craver
Perfect! Thanks!
Abe Miessler
@Abe - Welcome :)
Nick Craver
A: 

If you need to do more intricate formatting then changing the display of a date, you can also use 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;
    }
}
TheGeekYouNeed