indofraiser indofraiser - 6 months ago 97
Vb.net Question

If Else Statement -- 'If' operator requires either two or three operands

Aim: To show comments, but limit to 25 characters plus 3 dots. The dots would only show if over 25 characters. In addition we would show a tooltip so the user can see the full text if required.

I have tried with and without the tooltip. There's no error in Visual Studio just on the website.

Issue: 'If' operator requires either two or three operands.

Code:

<asp:TemplateField HeaderText="Recommendations" SortExpression="Recommendations">
<ItemTemplate>
<asp:Label ID="lblRecommendations" runat="server" Text='<%# If(Len(Eval("Recommendations").ToString()) > 25, Left(Eval("Recommendations").ToString(), 25) + "...", Eval("Recommendations").ToString() %>' ToolTip='<%# Eval("Recommendations") %>' />
</ItemTemplate>
</asp:TemplateField>


Notes:

I have some working If statements already which work such as the below so assume I have a comma or bracket misplaced?;

<asp:TemplateField HeaderText="Next Survey Due" SortExpression="NEXTSURVEYDUE">
<ItemTemplate>
<asp:Label ID="lblNEXTSURVEYDUE" runat="server" Text='<%# If(Eval("NEXTSURVEYDUE", "{0:d}") = "01/01/1900", " ", Eval("NEXTSURVEYDUE", "{0:d}")) %>' />
</ItemTemplate>
</asp:TemplateField>

Answer

The Text code as listed in your question is missing a closing bracket:

If(Len(Eval("Recommendations").ToString()) > 25, Left(Eval("Recommendations").ToString(), 25) + "...", Eval("Recommendations").ToString()

As a side-note, you should use the &hellip; (…) HTML entity instead of 3 dots, it only takes a single character and usually takes up less space.