# Eval if declaración en repetidor


Estoy tratando de comprobar un valor de cadena dentro de un repetidor, y si tiene valor entonces escribir un enlace, pero parece que no puede conseguir que funcione. Si hay un valor en myUrl entonces quiero mostrar el vínculo.

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

¿Alguien puede ayudar?

Author: Wildcat, 2012-02-15

6 answers

Pruebe este código !!!

<%#Eval("myURL").ToString().Length > 0 ?
"<a  title='myTitle' target='_blank' href='<%# Eval("myURL") %>'>my link</a>":""%>
 26
Author: Madhu,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2017-12-08 16:55:46

Personalmente odio usar lógica condicional como esa en la página.

Hay dos opciones que creo que son mejores. Puede tener un control de hipervínculo en el repetidor y establecer la visibilidad dependiendo de si el param de myURL está allí.

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

O lo que puedes hacer es tener un método en tu código detrás al que volver a llamar con el parámetro "myURL".

Por ejemplo

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

    return string.Empty;
}

Y llamar a ASPX

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

NB este es un código no probado, pero esta es la forma en que lo hago normalmente este tipo de cosas.

 20
Author: Dave Walker,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2012-02-15 08:29:11

Usaría la cadena.Formatee e incluya el HTML como parte de la cadena. Es cierto que no es la pieza de código más limpia jamás escrita, pero en mi opinión es la mejor opción:

Por ejemplo, lo siguiente generará una etiqueta de anclaje si la propiedad Url existe, de lo contrario generará un span.

<%# string.Format(Eval("Url") != null ? "<a href=\"{0}\">{1}</a>" : "<span>{1}</span>", Eval("Url"), Eval("Text")) %>">
 5
Author: Kevin Farrugia,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2013-07-26 14:37:06

Intente agregar un runat="server" y luego agregue un bloque de script para la (nueva) propiedad visible del lado del servidor:

 <a  title="myTitle" target="_blank" href="<%# Eval("myURL") %>" runat="server" visible='<%#Eval("myURL").ToString().Length > 0 %>'>my link</a>
 3
Author: pete,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2012-02-15 08:31:30
 1
Author: ravidev,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2017-05-23 11:47:19

También puede llamar a su función pública dentro del código detrás del archivo:

<%# MyFunction(Eval("myURL").ToString().Length) %>
 0
Author: numbtongue,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2017-02-14 23:36:18