基于服务器的控件对我来说并不是很好的解决方案,因为我的面板默认情况下应始终包含一个asp 复选框,该复选框允许用户隐藏和显示面板内容。创建自定义面板控件
我创建了我的面板作为模板用户控件,但现在我有问题,我无法在其中声明变量。
[ParseChildren(true)]
public partial class MyPanel: System.Web.UI.UserControl
{
private ITemplate messageTemplate = null;
[TemplateContainer(typeof(MessageContainer))]
[PersistenceMode(PersistenceMode.InnerProperty)]
public ITemplate Content
{
get
{
return messageTemplate;
}
set
{
messageTemplate = value;
}
}
void Page_Init()
{
MessageContainer container = new MessageContainer();
messageTemplate.InstantiateIn(container);
PlaceHolder1.Controls.Add(container);
}
[ParseChildren(true)]
public class MessageContainer : Control, INamingContainer
{
internal MessageContainer()
{
}
}
}
如果我在MyPage.aspx
以下则控制定义不插入MyPage.aspx.designer.cs
一个他们做通常:
<my:MyPanel>
<Content>
<asp:TextBox id = "foo" runat="server" />
</Content>
</my:MyPanel>
因此foo
不是由设计者作为控制变量创建的,因此,我无法访问它。
如何创建自己的面板,允许在其中声明控件?
编辑: 我现在试用[ParseChildren(false)]
。包含变量的变量现在在表单的设计器代码中生成。现在的问题是messageTemplate.InstantiateIn(container)
引发异常。
可能的重复http://stackoverflow.com/questions/306288/custom-asp-net-container-control? – 2010-10-01 11:59:16
没有我的问题是不同的,至少我不会接受这个问题的答案。 – codymanix 2010-10-01 12:14:53
[我可以用asp.net usercontrols定义我自己的面板控件]可能的重复(http://stackoverflow.com/questions/3829728/can-i-define-my-own-panel-control-with-asp-net -usercontrols) – RPM1984 2010-10-01 12:33:03