Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I format the text in a databound TextBox?

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.

like image 281
Abe Miessler Avatar asked Mar 22 '10 18:03

Abe Miessler


2 Answers

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}") %>' />
like image 191
Nick Craver Avatar answered Sep 20 '22 22:09

Nick Craver


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;
    }
}
like image 1
TheGeekYouNeed Avatar answered Sep 21 '22 22:09

TheGeekYouNeed