开发者

How can I format the text in a databound TextBox?

开发者 https://www.devze.com 2022-12-23 14:48 出处:网络
I have ListView that has the following EditItemTemplate: <EditItemTemplate> <tr style=\"\">

I have ListView that has the following EditItemTemplate:

<EditItemTemplate>
    <tr style="">
        <td>
            <asp:LinkButton ID="UpdateButton" runat="server" Command开发者_运维百科Name="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.


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}") %>' />


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;
    }
}
0

精彩评论

暂无评论...
验证码 换一张
取 消