views:

133

answers:

1

I have seen two suggestions for my original question about whether it is possible to define a content area inside a user control and there are some helpful suggestions i.e.

http://stackoverflow.com/questions/1971498/passing-in-content-to-asp-net-user-control

and

http://stackoverflow.com/questions/1912283/asp-net-user-control-inner-content

Now, I like the theory of the latter better than the former just for aesthetic reasons. It seems to make more sense to me but the example given uses two variables content and templateContent that the answerer has not defined in their example code. Without these details I have found that the example does not work. I guess they are properties of the control? Or some such?

EDIT - DETAILS: What I am trying to do

I have need of an ASP.Net user control that conceals some content in a panel inside a placeholder and asks for the input of a code in a visible panel.

Essentially the user will put their code into the provided textbox in Panel A and submit it, it will be checked and, if it is valid, panel B and the locked content will be displayed.

I have done a test where the content was hard coded into panel B but as soon as I need to make the content a generic input it fails. If it were just text or somesuch then I could make it a property of the control, but as it is, in fact, another User Control I am having some difficulty getting this into the "hidden" panel.

Any other workable solutions are also welcome.

EDIT NOTE: The solution I'm trying to implement this in 2.0 I did find a 3.5 solution which I cannot use.

The former example seems workable but I'd prefer to go with the latter if someone could fill in the blanks for me.

Thanks.

+1  A: 

Okay, so this is disturbingly easy but many of the tutorials on the web that talk about this kind of thing push to do extravagant things that require the control to parse ListItems or such.

So this solution is purely so that you can build a control that, for whatever reason, has a placeholder in it that could have anything inside it (kind of like a content area on a Master page). In this instance it happens to be because the Panel containing the placeholder is hidden until appropriate input actions have taken place in another panel.

First, you need to add this:

[ParseChildren(true,"Content")]
[PersistChildren(false)]

just above the part of the control which looks like this:

public partial class MyControl : System.Web.UI.UserControl

then in the control scoped declarations at the head of the control you want to declare thus:

private Control _content;
public Control Content { get { return _content; } set { _content = value; } }

Finally you need to place the content into the placeholder like this:

phContent.Controls.Add((Control)_content);

That last line goes into the Page_Init event. For reference "phContent" is the name of the place holder where you want the content to appear, like this:

<asp:Panel ID="pnlLockable" runat="server" Visible="False">
<asp:Placeholder runat="server" ID="phContent" />
</asp:Panel>

On the front end the resulting implementation looks like this:

<uc:MyControl runat="server" ID="lockit1">
<Content>
//...your stuff goes here...
</Content>
<uc:MyControl>

Note that I presume that what is inbetween the Content Tags is a root control. This is because I nested another user control in there. I imagine if you put whatever content you want within a panel or placeholder it should be fine.

bert
+1 for using the terms "disturbingly easy".
Chris Lively
Thanks. I often find that in trying to do something reasonably complex online tutorials often render explanations of the simple obtuse in the extreme. In the end this was farly obvious once you understood the underlying concepts but I hadn't really ever thought about the concepts so all the tutorials that show you how to build template repeaters and bespoke lists were just not appropriate because I couldn't tell what the bare minimum implementation was.Hence, disturbingly easy.
bert