相关文章推荐
失眠的玉米  ·  mybatis mysql ...·  2 年前    · 
Collectives™ on Stack Overflow

Find centralized, trusted content and collaborate around the technologies you use most.

Learn more about Collectives

Teams

Q&A for work

Connect and share knowledge within a single location that is structured and easy to search.

Learn more about Teams <% if(Something) { %> <div id="someUniqueMarkup"> This markup should not be output if Something==true. <units:MyUserControl runat="server"/> else { %> <units:MyUserControl runat="server" /> <% } %>

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?

why not use codebehind? your control is server side, so when adding them ID parameter, you can set your setting. and use it only once, as mentioned @Kenneth in his answer. sasjaq May 29, 2013 at 8:42 in·tu·i·tive /inˈt(y)o͞oitiv/ Adjective Using or based on what one feels to be true even without conscious reasoning; instinctive. (chiefly of computer software) Easy to use and understand. I'm not a "liar" - it's not intuitive. Which programming language executes something in an if-statement even though the condition is false? Answer: pretty much none, apart from WebForms, in this example. theyetiman May 29, 2013 at 10:41 @GrantThomas That code inside a block is executed even when the block "is not executed" is as far from intuitive as it gets if you ask me. Arve Systad May 29, 2013 at 10:44 I'd actually disagree. Or, it is a sweeping generalization. But it's true. How intuitive is the entire page lifecycle? Add in databinding and databinding events. And those dirty, generated designer.cs-files. How is all that intuitive ? Nobody can understand those things without specific knowledge of how Webforms work, while most other frameworks can usually be understood by knowing the language and the major patterns used. Arve Systad May 29, 2013 at 11:18

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:

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

EDIT : Answer to edit in the original post:

<% if(Something) { %> <div id="someUniqueMarkup"> This markup should not be output if Something==true. <asp:placeholder id="phItemInDiv" runat="server" /> else { %> <asp:placeholder id="phItemOutsideDiv" runat="server" /> <% } %> 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

I know, but there is a lot of stuff that's supposed to not be shown around the user control. Will update original post to clarify this. Arve Systad May 29, 2013 at 8:39 See edits now. The SomeSetting attribute was not really my point at all, it was just added to make more of a difference between them. :-\ Arve Systad May 29, 2013 at 8:44 The basic point remains the same, if you put the user control on the page it will execute its lifecycle entirely. I edited my answer to show you how to prevent this. (note: if you need to edit properties of the usercontrol, you need to cast the result of LoadControl to your usercontrol, set the properties and then add it to the placeholder Kenneth May 29, 2013 at 8:45 Fair enough. But then my basic point remains the same too: This is just an unintuitive interface for programmers using ASP.NET :-) Conditionally loading the control in codebehind with LoadControl actually seems like the solution I actually want here, aside from that it's really dirty (string reference to a user control). Arve Systad May 29, 2013 at 8:47

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

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

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.

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.

See edit, I removed the SomeSetting property since it wasn't really part of the point i was trying to make. – Arve Systad May 29, 2013 at 8:48

Thanks for contributing an answer to Stack Overflow!

  • Please be sure to answer the question. Provide details and share your research!

But avoid

  • Asking for help, clarification, or responding to other answers.
  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.