Note that there are some explanatory texts on larger screens.

plurals
  1. POFormatting DataBinder.Eval data
    primarykey
    data
    text
    <p>How can I format data coming from a DataBinder.Eval statement in an ASPX page?</p> <p>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.</p> <p>The code for this goes like this:</p> <pre><code>&lt;asp:Repeater ID="Repeater1" runat="server" DataSourceID="ObjectDataSource1"&gt; &lt;HeaderTemplate&gt;&lt;table cellpadding="0" cellspacing="0" width="255"&gt;&lt;/HeaderTemplate&gt; &lt;ItemTemplate&gt; &lt;tr&gt;&lt;td &gt; &lt;a href='/content/latestNews.aspx?id=&lt;%#DataBinder.Eval(Container.DataItem, "id") %&gt;'&gt; &lt;asp:Label ID="lblNewsTitle" runat="server" Text='&lt;%# DataBinder.Eval(Container.DataItem, "title") %&gt;'&gt;&lt;/asp:Label&gt; &lt;/a&gt; &lt;/td&gt;&lt;/tr&gt; &lt;tr&gt;&lt;td&gt; &lt;asp:Label ID="lblNewsDate" runat="server" Text='&lt;%# DataBinder.Eval(Container.DataItem, "publishedDate"))%&gt;'&gt;&lt;/asp:Label&gt; &lt;/td&gt;&lt;/tr&gt; &lt;/ItemTemplate&gt; &lt;FooterTemplate&gt;&lt;/table&gt;&lt;/FooterTemplate&gt;&lt;/asp:Repeater&gt; </code></pre> <p>Is there a way I could call a custom method with the DataBinder.Eval value as its parameter (something like below)?</p> <pre><code>&lt;asp:Label ID="lblNewsDate" runat="server" Text='&lt;%# GetDateInHomepageFormat(DataBinder.Eval(Container.DataItem, "publishedDate")) )%&gt;'&gt;&lt;/asp:Label&gt; </code></pre> <p>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?</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload