MaxCoder88
MaxCoder88

Reputation: 2428

Eval() event problem

As you can see, if the condition is true, I attempt to use substring command to get 20 character. But it doesn't agree.

'<%#IIf(Eval("haber").ToString().Length >= 20, Eval("haber").ToString().Substring(0, 20) + "...", Eval("haber").ToString )%>'

Also, I'm using that code on onmouseover event.

Like this:

<a href="" onmouseover=""/>

What might be the problem?

Upvotes: 0

Views: 123

Answers (2)

Tim
Tim

Reputation: 28530

IIf does not do short-circuit logic - in other words, all three arguments get evaluated, whether or not the first argument is true. In your case, you didn't specify what the result was, but I'm betting you always get the full string, even when the string length > 20.

You can either do as Samich suggested, or you can use the new If statement, which does implement short-circuiting:

<%#IIf(Eval("haber").ToString().Length >= 20, Eval("haber").ToString().Substring(0, 20) + "...", Eval("haber").ToString())%>

I think Samich's approach is better (less code in your markup), but if you want to leave it in the markup, you should use the If operator:

<%#If(Eval("haber").ToString().Length >= 20, Eval("haber").ToString().Substring(0, 20) + "...", Eval("haber").ToString())%>

IIf Function

If Operator

Upvotes: 0

Samich
Samich

Reputation: 30145

First of all: move your logic from markup to the method at codebehind and call this method in markup.

Markup:

<%# this.FormatValue(Eval("haber")) %>

Codebehind:

public string FormatValue(object value)
{
    string str = (string)value;

   if (str.length > 20)
     return str.Substring(0, 20) + "...";

   return str;
} 

Second: you have syntax error in your expression: in the last ToString missing ()

Upvotes: 1

Related Questions