ASP.Net: User control with content area, it#39;s clearly possible but I need some details(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.
将内容传递给 ASP.NET 用户控件
和
ASP.NET 用户控件内部内容
现在,出于美学原因,我喜欢后者的理论比前者更好.这对我来说似乎更有意义,但给出的示例使用了回答者未在其示例代码中定义的两个变量 content 和 templateContent.如果没有这些细节,我发现该示例不起作用.我猜它们是控件的属性?还是这样的?
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?
编辑 - 详细信息:我要做什么
我需要一个 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.
基本上用户将他们的代码放入面板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.
我已经完成了一项测试,其中内容被硬编码到面板 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.
前一个例子似乎可行,但如果有人可以为我填补空白,我更愿意使用后者.
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>
请注意,我假设内容标签之间的内容是根控件.这是因为我在那里嵌套了另一个用户控件.我想如果你把你想要的任何内容放在一个面板或占位符中应该没问题.
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:内容区域的用户控制,这显然是可能的,但我需要一些细节的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:ASP.Net:内容区域的用户控制,这显然是可能的,但我需要一些细节
基础教程推荐
- 有没有办法忽略 2GB 文件上传的 maxRequestLength 限制? 2022-01-01
- 如何激活MC67中的红灯 2022-01-01
- 为什么Flurl.Http DownloadFileAsync/Http客户端GetAsync需要 2022-09-30
- SSE 浮点算术是否可重现? 2022-01-01
- rabbitmq 的 REST API 2022-01-01
- 如何在 IDE 中获取 Xamarin Studio C# 输出? 2022-01-01
- c# Math.Sqrt 实现 2022-01-01
- 将 XML 转换为通用列表 2022-01-01
- 将 Office 安装到 Windows 容器 (servercore:ltsc2019) 失败,错误代码为 17002 2022-01-01
- MS Visual Studio .NET 的替代品 2022-01-01