ASP.NET 3.5 GridView 模板字段显示日期如果不是 MinValue?
我有一个关于 ASP.NET 的相当简单的问题。我有一个带有模板字段的网格视图。在模板字段中我有一个标签。我尝试将标签文本属性设置为绑定列中指定的日期如果日期时间值不是最小值(换句话说,日期时间字段尚未设置)。我真的不知道该怎么做。我在下面给出了一些我尝试过的代码,以便更清楚地了解我想要完成的任务:
<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>
这是按要求提供的对象数据源的代码:
<asp:ObjectDataSource ID="projectDataSource" runat="server"
SelectMethod="GetProjects" TypeName="Lemur.Services.Impl.ProjectService">
</asp:ObjectDataSource>
这是从对象数据源返回的类:
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
}
我尝试检索的字段是 StartDate。
有人对如何实现这一目标有任何想法吗?
感谢您的任何帮助。
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
在使用 Project 类代码更新我的帖子后,我意识到尝试将 if 语句集成到 TemplateField 中似乎很草率。我决定向 Project 类添加一个名为 StartDateString 的属性,其中包含我想要在视图层执行的逻辑。这样我就可以为 StartDateString 属性创建一个 Bound 字段:
我猜我是从错误的方向解决问题的......
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:
Guess I was approaching the problem from the wrong direction....
我意识到这篇文章很旧,但我也遇到了同样的问题(过去曾这样做过,但我忘记了如何解决它)。在其他地方找到解决方案,认为可能对其他寻求相同解决方案的人有用。无论如何......
在你的类中使用可为空的 DataTime 类型。 “日期时间?”而不是“日期时间”
I realise this post is old, but I just had the same issue (have done so in the past but I'd forgotten how to solve it). Found solution elsewhere put thought might be of use for others seeking the same. Anyhow....
Use a nullable DataTime type in your class. "DateTime?" instead of "DateTime"