如何有条件地控制ASP.NET中控件的可见性? [英] How do I conditionally control the visibility of a control in ASP.NET?

查看:93
本文介绍了如何有条件地控制ASP.NET中控件的可见性?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个asp:Image。我希望仅在<%#Eval( Image)%> 不是 null 。

I've got an asp:Image. I want this control to display, only if <%#Eval("Image")%> is not null.

我不知道如何写此条件语句。我要说的是这样的东西(如果从数据结构中获取的图像的值不为null,则显示图像。否则,不显示):

I do not know how to write this conditional statement. What I'm trying to say is something like this (if the value of 'image' taken from the data structure is not null, then display the image. Otherwise, do not):

<%#Eval("Image")%> != 0 ? <asp:Image ID="image" runat="server"/>

我知道这不是语法-将其视为伪代码,因为我从未有过在标记中写条件语句。

I'm aware that this is not the syntax - consider it as pseudocode, as I have never had to write a conditional statement in the markup.

有什么想法吗? :)

推荐答案

您可以将控件的 Visible 属性绑定到表达式并在页面加载时调用 DataBind()

You can bind the Visible property of your control to the expression and call DataBind() while the page is loading:

<asp:Image runat="server" id="image" Visible='<%#Eval("Image") != null %>' />

如果您不使用服务器控件并希望显示/隐藏简单标记,则只需将其括起来在 if 语句中:

If you are not using server controls and want to show/hide simple markup, you can simply enclose it in an if statement:

<% if ( condition ) { %>
    <img src='<%= linkToImageSource %>' />
<% } %>

这篇关于如何有条件地控制ASP.NET中控件的可见性?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆