C# aspx文件中的if语句

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16809017/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-10 07:49:56  来源:igfitidea点击:

If statements in aspx files

c#asp.netwebforms

提问by Arve Systad

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_Loadetc is run once for each control that is actually on the page.

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

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.

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

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?

只是我,还是这只是一个非常不直观的界面?并且只要它在页面上,实际上可能根本不加载/执行用户控件吗?

采纳答案by Kenneth

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:

由于页面上有两个控件,它将同时呈现它们。您创建的 if-check 仅确定它是否包含在输出中。防止这种情况的最简单方法是像这样更改代码:

<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 Somethingis true

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

回答by SiN

The best way, in my opinion, is to declare your user control once in the ASPX.

在我看来,最好的方法是在 ASPX 中声明一次您的用户控件。

In the code behind, on PageLoad, apply the logic you see fit:

在后面的代码中,在 PageLoad 上,应用您认为合适的逻辑:

if(something)
    MyUserControl.SomeSettings = ...

If there is a problem in the chronology, do the above logic in PreLoad since it will fire before Page Load of the page and all of its related user controls.

如果时间顺序有问题,请在 PreLoad 中执行上述逻辑,因为它将在页面及其所有相关用户控件的页面加载之前触发。

EDIT:

编辑:

You can put two different IDs on the user controls with Enabled = false. In Page_load, set Enabled to one of them based on the logic you desire.

您可以使用 Enabled = false 在用户控件上放置两个不同的 ID。在 Page_load 中,根据您需要的逻辑将 Enabled 设置为其中之一。