在用户控件内有条件地显示数据

发布于 2024-08-20 01:32:31 字数 449 浏览 2 评论 0原文

在 ASP.NET 用户控件中,我有这样一行:

<div>Web: <a href="<%# Eval("Web") %>"><%# Eval("Web") %></a></div>

我想更改它,以便仅在 Web 有值时才呈现此 HTML。

我尝试在服务器端脚本中包装 String.IsNullOrEmpty(Eval("Web") as string)Eval 只能在“绑定”标签内使用

最好的方法是什么?

Within an ASP.NET user control I have the line:

<div>Web: <a href="<%# Eval("Web") %>"><%# Eval("Web") %></a></div>

I'd like to change it so this HTML is only rendered if Web has a value.

I've tried wrapping String.IsNullOrEmpty(Eval("Web") as string) in server side script but Eval can only be used inside a "binding" tag.

What is the best way to do this?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

神也荒唐 2024-08-27 01:32:31

这是一种解决方法,但您可以在 ItemTemplate 标记中设置一个隐藏字段:

<asp:HiddenField ID="HiddenField1" runat="server" Value='<%# Eval("web") %>' />

然后您可以将 div 的“runat”属性设置为“server”并为该 div 提供一个 ID。

<div id="divWeb" runat="server" visible="false">Web: <a href="<%# Eval("Web") %>"><%# Eval("Web") %></a></div>

在代码隐藏中,检查 HiddenField1 是否为空。如果不为空,则设置 'divWeb' visible = true

此方法的缺点是用户可以手动更改 HiddenField1 值。但是,如果这不是问题(安全方面),那么您可以尝试此方法。

更新
下面的代码片段来自 此网站

<asp:Repeater id="collectionRepeater" Runat="server">  
     <ItemTemplate>
      <%# DataBinder.Eval(Container.DataItem, "OwnerId") %> - 
      <asp:literal ID="see" Runat="server" 
         Visible='<%# (int)DataBinder.Eval(Container.DataItem, "Pets.Count") > 0 %>'>
         see pets
      </asp:Literal>
      <asp:literal ID="nopets" Runat="server" 
        Visible='<%# (int)DataBinder.Eval(Container.DataItem, "Pets.Count") == 0 %>'>
          no pets
       </asp:Literal>
       <br />
      </ItemTemplate>
    </asp:Repeater>

此中还有其他选项线程

It's a bit of a workaround, but you could have a hidden field in your ItemTemplate tag:

<asp:HiddenField ID="HiddenField1" runat="server" Value='<%# Eval("web") %>' />

You could then set the 'runat' attribute of the div to 'server' and give the div an ID.

<div id="divWeb" runat="server" visible="false">Web: <a href="<%# Eval("Web") %>"><%# Eval("Web") %></a></div>

In your code-behind, you check whether or not HiddenField1 is empty. If it's not empty, then set 'divWeb' visible = true.

The downside to this method is that the user could manually change the HiddenField1 value. However, if that's not a problem (security wise), then you could try this method.

Update
The code snippet below is from the inline section of this site:

<asp:Repeater id="collectionRepeater" Runat="server">  
     <ItemTemplate>
      <%# DataBinder.Eval(Container.DataItem, "OwnerId") %> - 
      <asp:literal ID="see" Runat="server" 
         Visible='<%# (int)DataBinder.Eval(Container.DataItem, "Pets.Count") > 0 %>'>
         see pets
      </asp:Literal>
      <asp:literal ID="nopets" Runat="server" 
        Visible='<%# (int)DataBinder.Eval(Container.DataItem, "Pets.Count") == 0 %>'>
          no pets
       </asp:Literal>
       <br />
      </ItemTemplate>
    </asp:Repeater>

There are also alternative options in this thread

少女的英雄梦 2024-08-27 01:32:31

嗯,MVC 更多的是针对页面中的这种类型的逻辑...通常使用 Web 表单,一切都是通过代码隐藏完成的...此外,您是否会考虑执行以下操作:

<div style='<%# ((Eval("Web") != null) ? "display" : "none") %>'>Web: <a href="<%# Eval("Web") %>"><%# Eval("Web") %></a></div>

尚未专门尝试过这种方法,但我知道第三级(?:)在这种情况下起作用,因此它可以起作用似乎是合乎逻辑的......

Well, MVC was meant more for that type of logic in the page... typically with web forms everything is done with code-behind... Additionally, would you consider doing something like:

<div style='<%# ((Eval("Web") != null) ? "display" : "none") %>'>Web: <a href="<%# Eval("Web") %>"><%# Eval("Web") %></a></div>

Haven't tried this approach specifically, but I know tertiary (?:) works in this context, and so it seems logical that it could work....

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文