0

希望 ASP.net 专家可以在这里帮助我,这很简单,但我一直在玩弄语法足够长的时间,知道我不知道。

我只是想在页面上内联 if 块。如果列表是赞助的,我想显示他们的网站地址。这个语法是什么样的?

//This is what I've been getting at:

    <% if(Eval("Sponsored_Listing")=="Yes") {
         //Then I want to simply write the URL on the page.
         <a href="http://<#% Eval("Website") %>"><%# Eval("Website") %></a>
    else { 
         //Do nothing -- nothing is written to screen.
    }    
%> 

我觉得这是预处理器指令的问题。先感谢您。

4

2 回答 2

0

Eval返回对象,因此您需要强制转换为字符串。

<%# Eval("Sponsored_Listing").ToString() =="Yes" ? 
    string.Format("<a href=\"http://{0}\">{0}</a>", Eval("Website")) : "" %>

另一个想法- 如果你想执行一个逻辑,你想建议使用ItemDataBound方法。它更清晰并且支持强类型。

于 2013-05-16T17:33:10.967 回答
0

这是正确的方法:

 <%if (Eval("Sponsered_Listing") == "Yes")

      { 
      //write the Url

          Response.Write("<a href='Http://" + Eval("WebSite")  + "'>" + Eval("WebSite") + "</a>");

      }

问候

于 2013-05-16T17:23:49.547 回答