格式化 DataBinder.Eval 数据

1

本文介绍了格式化 DataBinder.Eval 数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

问题描述

限时送ChatGPT账号..

如何格式化来自 ASPX 页面中 DataBinder.Eval 语句的数据?

How can I format data coming from a DataBinder.Eval statement in an ASPX page?

例如,我想在主页中以特定格式显示新闻项目的发布日期.我正在使用 ASP.NET 2.0 Repeater 控件来显示新闻项目列表.

For example, I want to display the published date of the news items in a particular format in the homepage. I'm using the ASP.NET 2.0 Repeater control to show the list of news items.

代码如下:

<asp:Repeater ID="Repeater1" runat="server" DataSourceID="ObjectDataSource1">
<HeaderTemplate><table cellpadding="0" cellspacing="0" width="255"></HeaderTemplate>
<ItemTemplate>
    <tr><td >
            <a href='/content/latestNews.aspx?id=<%#DataBinder.Eval(Container.DataItem, "id") %>'>
                <asp:Label ID="lblNewsTitle" runat="server" Text='<%# DataBinder.Eval(Container.DataItem, "title") %>'></asp:Label>
            </a>
    </td></tr>
    <tr><td>
           <asp:Label ID="lblNewsDate" runat="server" Text='<%# DataBinder.Eval(Container.DataItem, "publishedDate"))%>'></asp:Label>
    </td></tr>
</ItemTemplate>
<FooterTemplate></table></FooterTemplate></asp:Repeater>

有没有一种方法可以调用自定义方法,并将 DataBinder.Eval 值作为其参数(如下所示)?

Is there a way I could call a custom method with the DataBinder.Eval value as its parameter (something like below)?

<asp:Label ID="lblNewsDate" runat="server" Text='<%# GetDateInHomepageFormat(DataBinder.Eval(Container.DataItem, "publishedDate")) )%>'></asp:Label>

如果是,那么我在哪里编写 GetDateInHomepageFormat 方法?我在页面后面的代码中进行了尝试,但出现了运行时错误?如果这不可能,有没有办法进行内联格式化?

If yes, then where do I write the GetDateInHomepageFormat method? I tried out in the code behind page but got a run time error? If this is not possible, is there a way to do inline formatting?

推荐答案

DataBinder.Eval 有一个可选的重载来提供格式:

There is an optional overload for DataBinder.Eval to supply formatting:

<%# DataBinder.Eval(Container.DataItem, "expression"[, "format"]) %>

格式参数是一个字符串值,使用值占位符替换语法(称为复合格式)如下:

The format parameter is a String value, using the value placeholder replacement syntax (called composite formatting) like this:

<asp:Label id="lblNewsDate" runat="server" Text='<%# DataBinder.Eval(Container.DataItem, "publishedDate", "{0:dddd d MMMM}") %>'</label>

这篇关于格式化 DataBinder.Eval 数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

The End

相关推荐

如何从 lambda 表达式中获取引用实例的实例
How to get the instance of a referred instance from a lambda expression(如何从 lambda 表达式中获取引用实例的实例)...
2023-11-11 C#/.NET开发问题
4

我可以使用 lambda 语法忽略委托参数吗?
Can I ignore delegate parameters with lambda syntax?(我可以使用 lambda 语法忽略委托参数吗?)...
2023-11-11 C#/.NET开发问题
7

由 C# 中的 lambda 创建的委托的生命周期是多少?
What is the lifetime of a delegate created by a lambda in C#?(由 C# 中的 lambda 创建的委托的生命周期是多少?)...
2023-11-11 C#/.NET开发问题
3

lambda 表达式的意义何在?
What#39;s the point of a lambda expression?(lambda 表达式的意义何在?)...
2023-11-11 C#/.NET开发问题
0

当作为普通委托参数提供时,为什么必须强制转换 lambda 表达式
Why must a lambda expression be cast when supplied as a plain Delegate parameter(当作为普通委托参数提供时,为什么必须强制转换 lambda 表达式)...
2023-11-11 C#/.NET开发问题
4

委托关键字与 lambda 表示法
delegate keyword vs. lambda notation(委托关键字与 lambda 表示法)...
2023-11-11 C#/.NET开发问题
2