Formatting DataBinder.Eval data

后端 未结 10 1127
眼角桃花
眼角桃花 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

    
    

    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.

提交回复
热议问题