如何在ASP.NET WebControl的“内容”内部属性中包含其他标记?
-
02-10-2019 - |
题
我已经搜索了该网站,但我找不到解决问题的解决方案,因此,如果已经回答过,则表示歉意(我确定以前有人必须问过这个问题)。
我写了一个我包装为WebControl和IscriptControl的jQuery弹出窗口。最后一步是能够在我的控件的标签中写下标记。我已经使用了几次InnerProperty属性,但仅用于包括强大的类别列表。
这是我在WebControl上的财产:
[PersistenceMode(PersistenceMode.InnerProperty)]
[DesignerSerializationVisibility(DesignerSerializationVisibility.Content)]
public something??? Content
{
get
{
if (_content == null)
{
_content = new something???();
}
return _content;
}
}
private something??? _content;
这是我所追求的HTML标记:
<ctr:WebPopup runat="server" ID="win_Test" Hidden="false" Width="100px" Height="100px"
Modal="true" WindowCaption="Test Window" CssClass="window">
<Content>
<div style="display:none;">
<asp:Button runat="server" ID="Button1" OnClick="Button1_Click" />
</div>
<%--Etc--%>
<%--Etc--%>
</Content>
</ctr:WebPopup>
不幸的是,我不知道我的内容属性应该是什么类型。我基本上需要复制 UpdatePanel
' ContentTemplate
.
编辑: :因此,以下允许自动创建模板容器,但是没有显示控件,我在做什么问题?
[PersistenceMode(PersistenceMode.InnerProperty)]
[DesignerSerializationVisibility(DesignerSerializationVisibility.Content)]
public ITemplate Content
{
get
{
return _content;
}
set
{
_content = value;
}
}
private ITemplate _content;
Edit2: :覆盖createChildControls允许渲染项目板中的控件:
protected override void CreateChildControls()
{
if (this.Content != null)
{
this.Controls.Clear();
this.Content.InstantiateIn(this);
}
base.CreateChildControls();
}
不幸的是,我现在无法从文件上的CodeBehind文件中访问Interplate中的控件。即,如果我在标记中放一个按钮,请这样:
<ctr:WebPopup runat="server" ID="win_StatusFilter">
<Content>
<asp:Button runat="server" ID="btn_Test" Text="Cannot access this from code behind?" />
</Content>
</ctr:WebPopup>
然后我无法访问 btn_Test
从背后的代码:
protected void Page_Load(object sender, EventArgs e)
{
btn_Test.Text = "btn_Test is not present in Intellisense and
is not accessible to the page. It does, however, render correctly.";
}
edit3: : 固定的!编辑2是正确的解决方案。只是Visual Studios 2010是臀部的痛苦。关闭该应用程序并重新打开它,并且在页面上可以访问内容属性中的所有控件。
Edit4: :编辑2没有解决问题。我已经尝试了 [TemplateInstance(TemplateInstance.Single)]
在任何人提到它之前的属性,但是当时我认为它没有改变。看来Visual Studios 2010今天很奇怪。
由于我删除了标签并继续工作,因此我认为该属性没有改变。由于再次返回代码,因此控件变得不可用。将属性添加回中,允许所有功能都可以正常工作,并使控件可以访问服务器端。 疯狂. 。当他在其他任何人面前提到修复程序时,我将接受布莱恩的回答。
解决方案
公共物品板内容
然后,您会喜欢UI:
Label label = new Label();
this.Content.InstantiateIn(label);
//Render label
编辑:确保模板还定义
[TemplateInstance(TemplateInstance.Single)]
因此,您可以直接访问模板中的控件。
其他提示
您应该尝试使用以下方式:
win_StatusFilter.FindControl("btn_Test") // this will be a Control
win_StatusFilter.FindControl("btn_Test") as Button // this will be a Button if control found, otherwise it will be null.
否则,您应该为您的控件定义一些属性,例如本文:http://msdn.microsoft.com/ru-ru/library/36574bf6%28v=vs.90%29.aspx
更新:根据本文中有关updatePanel的ContentTemplate属性的评论:
http://msdn.microsoft.com/en-us/library/system.web.ui.updatepanel.contenttemplate(v = vs.90).aspx
您可以从ContentTemplate获得控件,因为TemplateInstanceatTribute值 TemplateInstance.Single
).
因此,您只能使用此代码:
[PersistenceMode(PersistenceMode.InnerProperty)]
[DesignerSerializationVisibility(DesignerSerializationVisibility.Content)]
[TemplateInstance(TemplateInstance.Single)]
public ITemplate Content
更多信息,网址:
http://msdn.microsoft.com/ru-ru/library/system.web.ui.templateinstanceattribute(V=VS.90).aspx