ASP.Net:与内容区域用户的控制,它显然是可能的,但我需要一些细节 [英] ASP.Net: User control with content area, it's clearly possible but I need some details

查看:171
本文介绍了ASP.Net:与内容区域用户的控制,它显然是可能的,但我需要一些细节的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经看到了我对是否有可能定义用户控件中的内容区域原来的问题两个建议,有一些有用的建议,即

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.

<一个href=\"http://stackoverflow.com/questions/1971498/passing-in-content-to-asp-net-user-control\">http://stackoverflow.com/questions/1971498/passing-in-content-to-asp-net-user-control

<一个href=\"http://stackoverflow.com/questions/1912283/asp-net-user-control-inner-content\">http://stackoverflow.com/questions/1912283/asp-net-user-control-inner-content

现在,我喜欢比前者好于后者的理论只是为了美观的原因。这似乎更有意义,我,但这个例子采用两个变量的内容和templateContent的回答者并没有在他们的榜样code定义。没有这些细节的我已发现,例如不工作。我猜他们是控件的属性?或一些这样?

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?

编辑 - 详细:我所试图做的

我需要隐瞒的占位符内部在面板部分的内容,并要求为code的可见面板输入一个ASP.Net用户控件的。

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.

基本上,用户把自己的code到A组所提供的文本框,并提交它,它会进行检查,如果它是有效的,B组和锁定的内容将被显示出来。

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.

我做了测试,其中内容是很难coded进入B组,但只要我需要的内容的通用输入失败。如果只是文本或诸如此类,然后我可以使它控件的属性,但因为它是,其实,我有一些难以进入的隐藏面板这一点。另一个用户控件

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.

编辑注:2.0,我想实现这个解决方案我没有找到一个3.5的解决方案,我不能使用

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

前者例子似乎可行,但我想preFER与后者去,如果有人可以填补空白的我。

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

感谢。

推荐答案

好了,这是令人不安的是容易的,但许多谈论这种事情推到执行需要控制解析奢侈的事情在网络上的教程ListItems的或这样的。

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

这是最后一行进入Page_Init事件。参考phContent是你想要的内容出现,像这样的地方持有人的名称:

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>

请注意,我presume,什么是插图中的内容标签是根控制。这是因为我嵌套在有其他用户控件。如果你把你想要的任何内容的面板内或占位应该没事我想象的。

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.

这篇关于ASP.Net:与内容区域用户的控制,它显然是可能的,但我需要一些细节的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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