如果ASPX文件的语句 [英] If statements in aspx files

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

问题描述

我有一些code,基本上是这样的:

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>

根据的东西,其中之一是隐藏的,那就是罚款。但是,如果我设置在用户控件破发点,我注意到它加载两次(一次为每个上面的控制)和所有它的逻辑被运行两次。我当然可以用占位符或者多视图控制这一点,但同样的事情似乎也适用 - 的OnLoad / 的Page_Load 等为对于实际上是页面上的每个控件运行一次。

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.

编辑:
之所以即时显示/隐藏,这是因为我需要包括:如果控制的东西==真周围的一些标记。我可以换唯一标记本身的if-else之前和之后的控制,但这似乎只是肮脏的东西,如我上面想象真的应该那样简单。用户控件本身应该是完全两个场景的混乱财产它有相同的,对不起。

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?

推荐答案

既然你有两个页面上的控件将呈现他们俩。如果在检查你创建,只能确定它是否是包含在输出中。以prevent最简单的方法,这是改变你的code是这样的:

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);
}

这样,你将只有用户控件发出的(和加载),如果的东西是真的

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

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