Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Formatting DataBinder.Eval data

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

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.

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> 

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> 

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?

like image 955
Nahom Tijnam Avatar asked Nov 08 '08 20:11

Nahom Tijnam


People also ask

How do I change the date format on a repeater?

The DateTime field value is converted to dd/MM/yyyy by passing an additional parameter i.e. {0:dd/MM/yyyy} in order to format and display the DateTime field value in dd/MM/yyyy format.

What does Databinder Eval do?

Evaluates data-binding expressions at run time and formats the result as a string.

How does Eval work in asp net?

The Eval method takes the name of a data field and returns a string containing the value of that field from the current record in the data source. You can supply an optional second parameter to specify a format for the returned string.

What is Eval in C#?

Eval function is used to bind data to control inside a DataBound control, but it cannot update the values back to the database. Bind function can be used to bind data to control inside a DataBound control and also it can update the values back to the database. 3.


2 Answers

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> 
like image 193
DOK Avatar answered Sep 20 '22 16:09

DOK


After some searching on the Internet I found that it is in fact very much possible to call a custom method passing the DataBinder.Eval value.

The custom method can be written in the code behind file, but has to be declared public or protected. In my question above, I had mentioned that I tried to write the custom method in the code behind but was getting a run time error. The reason for this was that I had declared the method to be private.

So, in summary the following is a good way to use DataBinder.Eval value to get your desired output:

default.aspx

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

default.aspx.cs code:

public partial class _Default : System.Web.UI.Page {      protected string GetDateInHomepageFormat(DateTime d)     {          string retValue = "";          // Do all processing required and return value          return retValue;     } } 

Hope this helps others as well.

like image 40
Nahom Tijnam Avatar answered Sep 17 '22 16:09

Nahom Tijnam