#Eval if statement in repeater

♀尐吖头ヾ 提交于 2019-11-27 14:19:24
Madhu Beela

try this code !!!

<%#Eval("myURL").ToString().Length > 0 ?
"<a  title='myTitle' target='_blank' href='<%# Eval("myURL") %>'>my link</a>":""%>

I personally hate using conditional logic like that in the page.

There are two options that I think are better. You could have a Hyperlink control in the repeater - and set the visibility depending on if the myURL param is there.

visibility='<% #Eval("myURL").ToString().Length > 0 %>' 

OR what you can do is have a method on your code behind that you call back to with the "myURL" param.

E.g.

public string CreateURL(string myURL){
    if(!string.IsNullOrEmpty(myURL)){
       return "<a ... ";
    }

    return string.Empty;
}

And call in ASPX

<%# CreateURL(Eval("myURL").ToString()) %>

NB this is untested code but this is the ways I usually do this sort of thing.

I would use the String.Format and include the HTML as part of the string. Admittedly, it's not the neatest piece of code ever written, but in my opinion it's the best option:

For example the below will output an anchor tag if the property Url exists, otherwise it will output a span.

<%# string.Format(Eval("Url") != null ? "<a href=\"{0}\">{1}</a>" : "<span>{1}</span>", Eval("Url"), Eval("Text")) %>">

Try adding a runat="server" and then add a script block for the (new) server-side visible property:

 <a  title="myTitle" target="_blank" href="<%# Eval("myURL") %>" runat="server" visible='<%#Eval("myURL").ToString().Length > 0 %>'>my link</a>

You can also call your public function inside code behind file:

<%# MyFunction(Eval("myURL").ToString().Length) %>
标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!