开发者

Eval() event problem

开发者 https://www.devze.com 2023-04-02 12:41 出处:网络
As you can see, if the condition is true, I attempt to use substring command to get 20 character. But it doesn\'t agree.

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 pr开发者_开发技巧oblem?


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 ()


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

0

精彩评论

暂无评论...
验证码 换一张
取 消