Formatting DataBinder.Eval data

后端 未结 10 1090
眼角桃花
眼角桃花 2020-12-08 04:33

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 fo

相关标签:
10条回答
  • 2020-12-08 05:08

    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.

    0 讨论(0)
  • 2020-12-08 05:12

    This line solved my problem:

    <%#DateTime.Parse(Eval("DDDate").ToString()).ToString("dd-MM-yyyy")%>
    
    0 讨论(0)
  • 2020-12-08 05:12

    You can use it this way in aspx page

    <%# DataBinder.Eval(Container.DataItem, "DateColoumnName", "{0:dd-MMM-yyyy}") %>
    
    0 讨论(0)
  • 2020-12-08 05:14

    Why not use the simpler syntax?

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

    This is the template control "Eval" that takes in the expression and the string format:

    protected internal string Eval(
    string expression,
    string format
    

    )

    http://msdn.microsoft.com/en-us/library/3d2sz789.aspx

    0 讨论(0)
  • 2020-12-08 05:20

    Thanks to all. I had been stuck on standard format strings for some time. I also used a custom function in VB.

    Mark Up:-

    <asp:Label ID="Label3" runat="server" text='<%# Formatlabel(DataBinder.Eval(Container.DataItem, "psWages1D")) %>'/>
    

    Code behind:-

    Public Function fLabel(ByVal tval) As String
       fLabel = tval.ToString("#,##0.00%;(#,##0.00%);Zero")
    End Function
    
    0 讨论(0)
  • 2020-12-08 05:21

    Text='<%# DateTime.Parse(Eval("LastLoginDate").ToString()).ToString("MM/dd/yyyy hh:mm tt") %>'

    This works for the format as you want

    0 讨论(0)
提交回复
热议问题