How can I format the text in a databound TextBox?

时间:2021-12-21 00:37:07

I have ListView that has the following EditItemTemplate:

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

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:

我想格式化ExpectedAwardDateTextBox,因此它显示了一个较短的日期时间,但没有找到一种方法来做到这一点,而不进入后面的代码。在项目模板中,我有以下行来格式化标签中显示的日期:

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

我想找到一个与insertItemTemplate类似的方法。

2 个解决方案

#1


6  

You can use the Bind() overload like this:

您可以像这样使用Bind()重载:

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

Same for your Eval too:

您的Eval也是如此:

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

#2


1  

If you need to do more intricate formatting then changing the display of a date, you can also use OnItemDataBound

如果您需要执行更复杂的格式化,然后更改日期的显示,您还可以使用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;
    }
}

#1


6  

You can use the Bind() overload like this:

您可以像这样使用Bind()重载:

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

Same for your Eval too:

您的Eval也是如此:

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

#2


1  

If you need to do more intricate formatting then changing the display of a date, you can also use OnItemDataBound

如果您需要执行更复杂的格式化,然后更改日期的显示,您还可以使用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;
    }
}