开发者

ASP.net repeater problem

    <asp:Repeater runat="server" ID="QuestionList">
        <ItemTemplate>
            <tr align="center">
                <td><a href="HelpViewTicket.aspx?ID=<%# DataBinder.Eval(Con开发者_开发知识库tainer.DataItem, "ID") %>"><%# DataBinder.Eval(Container.DataItem, "ID") %></a></td>
                <td><a href="HelpViewTicket.aspx?ID=<%# DataBinder.Eval(Container.DataItem, "ID") %>"><%# DataBinder.Eval(Container.DataItem, "Subject")%></a></td>
                <td><abbr class="timeago" title="2008-07-17T09:24:17Z"><%# CommonFunctions.NiceDateTime(DataBinder.Eval(Container.DataItem, "DateSubmitted"))%></abbr></td>
                <td></td>                    
                <td><%# DataBinder.Eval(Container.DataItem, "Priority")%></td>
            </tr>
        </ItemTemplate>
    </asp:Repeater>

My repeater works fine, but how can I format data that it uses by passing it through functions? For example the line:

CommonFunctions.NiceDateTime(DataBinder.Eval(Container.DataItem, "DateSubmitted"))

Doesn't work as container doesn't exist within current context.


It does. The following works inside an ItemTemplate for me:

<%# Server.HtmlEncode((Container.DataItem as YourFancyDataSetType).Description) %>

Of course, 'YourFancyDataSetType' could be any type. Simply convert/cast the current DataItem to the underlying type and give it to any function you may like.

@Edit: typos


First of all, I think you can use just <%# Eval("ID") %> as a shortcut for <%# DataBinder.Eval(Container.DataItem, "ID") %> and it could clean up your code a lot. There shouldn't be any problem passing the result of an Eval into a method, the Eval should get evaluated before being passed in. I believe it gets passed in as either an object or a string, though; it's been a few years since I've done that.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜