格式化的DataBinder.Eval数据 [英] Formatting DataBinder.Eval data

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

问题描述

我怎样才能格式的数据从一个DataBinder.Eval的声明进来一个ASPX页面?

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.

在code这个是这样的:

The code for this goes like this:

<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方法?我在页面背后的code尝试了,但得到了一个运行时错误?
如果这是不可能的,是有办法做到行内格式化?

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"]) %>

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数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆