views:

105

answers:

1

Hi guys,

I have a rather simple problem with ASP.NET. I have a grid view with a template field. In the template field I have a label. I'm trying to set the labels text property to the date specified in a bound column if the datetime value is not the min value (in other words the date time field hasn't been set). I'm really not sure about how to go about this. I've given some code I've tried below, to be more clear in what I'm trying to accomplish:

        <asp:TemplateField HeaderText="Start Date">
            <ItemTemplate>
                <asp:Label ID="startDateLabel" runat="server"
                    Text='<%# if (Eval("StartDate") == DateTime.MinValue) { "None" } else { Eval("StartDate") } %>'>
                </asp:Label>
            </ItemTemplate>
        </asp:TemplateField>

Here is the code for my object data source as requested:

<asp:ObjectDataSource ID="projectDataSource" runat="server" 
    SelectMethod="GetProjects" TypeName="Lemur.Services.Impl.ProjectService">
</asp:ObjectDataSource>

Here is the class that is being returned from the object data source:

public class Project
{
    #region Private Fields

    private Client _client;

    #endregion

    #region Public Properties

    public Guid ID { get; set; }
    public string Name { get; set; }
    public string Abbreviation { get; set; }
    public string Description { get; set; }
    public Guid ClientID { get; set; }
    public Client Client
    {
        get
        {
            if (_client == null)
            {
                _client = new Client();
            }

            return _client;
        }
        set
        {
            _client = value;
        }
    }
    public ProjectStatus ProjectStatus { get; set; }
    public Employee ProjectManager { get; set; }
    public Guid ProjectManagerID { get; set; }
    public bool Active { get; set; }
    public DateTime StartDate { get; set; }
    public DateTime EndDate { get; set; }

    #endregion

    #region Overrides

    public override string ToString()
    {
        return Name;
    }

    #endregion
}

The field I am trying to retrieve is StartDate.

Anyone have any ideas on how to accomplish this?

Thanks for any help.

+1  A: 

After updating my post with the Project class code I realized that trying to integrate an if statement into the TemplateField seemed sloppy. I decided to add a property to the Project class called StartDateString, which contains the logic I wanted to perform at the view layer. This way I can just create a Bound field to the StartDateString property:

    public string StartDateString
    {
        get
        {
            if (StartDate == DateTime.MinValue)
            {
                return "";
            }
            else
            {
                return StartDate.ToString("dd/MM/yyyy");
            }
        }
    }

Guess I was approaching the problem from the wrong direction....

Brian D.
This is exactly what I was going to suggest. +1.
Matthew Jones