Question

I have a gridview that pulls a date from a database in one of it's columns:

<Columns>
   <asp:BoundField DataField="Person" HeaderText="Person" SortExpression="Person" />
   <asp:BoundField DataField="Effort" HeaderText="Effort" SortExpression="Effort" />
   <asp:BoundField DataField="Task" HeaderText="Task" SortExpression="Task" />
   <asp:BoundField DataField="Status" HeaderText="Status" SortExpression="Status" />
   <asp:BoundField DataField="OriginalEstimateHours" HeaderText="Estimated Hours" SortExpression="OriginalEstimateHours" />
   <asp:BoundField DataField="Total" HeaderText="Total" SortExpression="Total" />
   <asp:TemplateField HeaderText="Last Worked" ItemStyle-HorizontalAlign="Right" >
   <ItemTemplate>
       <asp:LinkButton ID="taskLinkButton" Text='<%# if (Eval("LastWorked").ToString().Length > 0) { Eval("LastWorked").ToString().Substring(0, 9)} %>' runat="server"  />
   </ItemTemplate>
       </asp:TemplateField>
</Columns>

This is pulling thetime back as well, all I want is the date so I am trying to add a substring. The problem is that some of the fields returned in the row have a date and some return NULL. So I want to make an if conditional which will make a substring only if length is > 0

However, I am getting an error that says 'Invalid expression term 'if'.

What am I doing wrong and how do I fix it?

Was it helpful?

Solution

I would suggest changing this into ternary operator:

<asp:LinkButton ID="taskLinkButton"
     Text='<%# (Eval("LastWorked").ToString().Length > 0) ? Eval("LastWorked").ToString().Substring(0, 9) : string.Empty %>'
     runat="server"  />

The reason why your approach is not working, I guess, is the fact that it is not actually returning anything.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top