aspx文件中的if语句 [英] If statements in aspx files

查看:31
本文介绍了aspx文件中的if语句的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一些基本上如下所示的代码:

<% if(Something) { %><div id="someUniqueMarkup">如果Something==true,则不应输出此标记.<units:MyUserControl runat="server"/>

<%}否则{%><units:MyUserControl runat="server"/><%}%>

根据Something,其中之一是隐藏的,这很好.但是如果我在用户控件中设置断点,我会注意到它被加载了两次(对于上面的每个控件一次)并且它的所有逻辑都运行了两次.我当然可以使用占位符或多视图来控制它,但同样的事情似乎也适用 - OnLoad/Page_Load 等为实际在页面上的每个控件运行一次.

我之所以显示/隐藏它,是因为如果 Something == true,我需要在控件周围包含一些标记.我可以在控制之前和之后将唯一标记"本身包装在 if-else 中,但这对于真正应该像我上面想象的那样简单的东西来说似乎很脏.用户控件本身在两种情况下应该完全相同,抱歉它具有令人困惑的属性.

是我一个人,还是这只是一个非常不直观的界面?是否真的有可能根本不加载/执行用户控件,只要它在页面上?

解决方案

因为页面上有两个控件,所以它会同时渲染它们.您创建的 if-check 仅确定它是否包含在输出中.防止这种情况的最简单方法是像这样更改代码:

<units:MyUserControl runat="server" SomeSetting="<%=Something %>"/>

EDIT:在原帖中回答

<% if(Something) { %><div id="someUniqueMarkup">如果Something==true,则不应输出此标记.<asp:placeholder id="phItemInDiv" runat="server"/>

<%}否则{%><asp:placeholder id="phItemOutsideDiv" runat="server"/><%}%>

MyUserControl ctrl = (MyUserControl)LoadControl("/pathtousercontrol.ascx")如果(某事){phItemInDiv.Controls.Add(ctrl);}别的{phItemOutsideDiv.Controls.Add(ctrl);}

这样,如果 Something 为 true,您将只发出(和加载)用户控件

I have some code that essentially looks like this:

<div>
    <% if(Something) { %>
        <div id="someUniqueMarkup">
            This markup should not be output if Something==true.

            <units:MyUserControl runat="server"/>
        </div>
    <% }
    else { %>
        <units:MyUserControl runat="server" />
    <% } %>
</div>

Depending on Something, one of them is hidden, and that is fine. But if I set break points in the user control, I notice it's being loaded twice (once for each of the controls above) and all it's logic is being run twice. I could of course control this with placeholders or multiviews, but the same thing seems to apply - OnLoad/Page_Load etc is run once for each control that is actually on the page.

EDIT: The reason why im showing/hiding this is because I need to include some markup around the control if Something == true. I could wrap the "unique markup" itself in if-else before and after the control, but that just seems dirty for something that really should be as simple as I've imagined above. The user control itself should be exactly the same in both scenarios, sorry for the confusing property it had.

Is it just me, or is this just a really unintuitive interface? And is it actually possible to not load/execute a user control at all as long as it's on the page?

解决方案

Since you have two controls on the page it will render them both. The if-check you create, only determines whether it's included in the output. The easiest way to prevent this is to change your code like this:

<div>
    <units:MyUserControl runat="server" SomeSetting="<%= Something %>" />
</div>

EDIT: Answer to edit in the original post:

<div>
    <% if(Something) { %>
        <div id="someUniqueMarkup">
            This markup should not be output if Something==true.

            <asp:placeholder id="phItemInDiv" runat="server" />
        </div>
    <% }
    else { %>
        <asp:placeholder id="phItemOutsideDiv" runat="server" />
    <% } %>
</div>



MyUserControl ctrl = (MyUserControl)LoadControl("/pathtousercontrol.ascx")
if (something){    
    phItemInDiv.Controls.Add(ctrl);
}
else{
    phItemOutsideDiv.Controls.Add(ctrl);
}

This way you will only have the user control emitted (and loaded) if Something is true

这篇关于aspx文件中的if语句的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
C#/.NET最新文章
热门教程
热门工具
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆