簡體   English   中英

如何在數據綁定TextBox中格式化文本?

[英]How can I format the text in a databound TextBox?

我有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類似的方法。

您可以像這樣使用Bind()重載:

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

您的Eval也是如此:

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

如果您需要執行更復雜的格式化,然后更改日期的顯示,您還可以使用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;
    }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM