Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ASP.Net: User control with content area, it's clearly possible but I need some details

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.

Passing in content to ASP.NET user control

and

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.

like image 769
bert Avatar asked Apr 27 '10 09:04

bert


People also ask

What is user control in ASP.NET with example?

The new ASP.NET user control is created and then opened in the designer. The markup for this new control is similar to the markup for an ASP.NET Web page, except that it contains an @ Control directive instead of an @ Page directive, and the user control does not have html, body, and form elements.

How pass data from ASPX page to user control?

Firstly we have to make a delegate, so create a delegate named SMTPH (string msg) just above the class because delegate always have to be declared above of the class. Create a variable name as msg for the delegate. Pass the TextBox value to the delegate. Write the code given below.


1 Answers

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;

[PersistenceMode(PersistenceMode.InnerProperty)]
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.

like image 132
bert Avatar answered Nov 16 '22 02:11

bert