在 ASP.NET 中创建自定义模板控件
我想创建自定义模板化控件,可以在页面中获取模板内部的控件(类似于 updatepanel 的行为)。因此,请提供更多详细信息。 该控件必须看起来像:
<ec:TabControl runat="server" ID="tab">
<Tabs>
<ec:Tab runat="server">
<TabContainer>
<asp:Button runat="server" Text="aaaaaa" />
</TabContainer>
<TabName>
text or controls
</TabName>
</ec:Tab>
<ec:Tab runat="server">
<TabContainer>
<asp:Button runat="server" Text="vcxvxvxv" />
</TabContainer>
<TabName>
some text
</TabName>
</ec:Tab>
</Tabs>
</ec:TabControl>
它以数据绑定控件运行的方式运行良好。换句话说,在 ondatabound 阶段,内部的所有控件和模板都会被适当地实例化。 但我真正想要的是能够直接从页面访问内部控件(通过 ID)。例如,您可以使用 updatepanel 来完成此操作(内容在页面范围内可用)。
下面您可以看到控件源代码的片段:
public class Tab
{
[PersistenceMode(PersistenceMode.InnerProperty)]
[TemplateContainer(typeof(HtmlAnchorContainer))]
[DesignerSerializationVisibility(DesignerSerializationVisibility.Content)]
public ITemplate TabName { get; set; }
[PersistenceMode(PersistenceMode.InnerProperty)]
[TemplateContainer(typeof(PanelContainer))]
[DesignerSerializationVisibility(DesignerSerializationVisibility.Content)]
public ITemplate TabContainer { get; set; }
}
public class TabControl : System.Web.UI.WebControls.WebControl
{
List<Tab> tabs;
[DesignerSerializationVisibility(DesignerSerializationVisibility.Content),
PersistenceMode(PersistenceMode.InnerProperty),
NotifyParentProperty(true)]
public List<Tab> Tabs
{
get { return tabs ?? new List<Tab>(); }
set { tabs = value; }
}
protected override void CreateChildControls()
{
base.CreateChildControls();
.....
foreach (Tab tabItem in Tabs)
{
//generating tree control for further rendering
}
...
}
protected override void OnDataBinding(EventArgs e)
{
EnsureChildControls();
base.OnDataBinding(e);
}
}
希望为您提供建议、建议、链接和建设性批评。))
I want to create custom templated control which controls inside of template coluld be got in page (a behavior similar to updatepanel). So, issue in more details.
The control has to look like:
<ec:TabControl runat="server" ID="tab">
<Tabs>
<ec:Tab runat="server">
<TabContainer>
<asp:Button runat="server" Text="aaaaaa" />
</TabContainer>
<TabName>
text or controls
</TabName>
</ec:Tab>
<ec:Tab runat="server">
<TabContainer>
<asp:Button runat="server" Text="vcxvxvxv" />
</TabContainer>
<TabName>
some text
</TabName>
</ec:Tab>
</Tabs>
</ec:TabControl>
It works well in way as databound control operates. In other words, during ondatabound stage all controls inside and templates are instanated appropriately.
But what i do want is to have access to controls inside and directly from page (by ID). For example, you can do it using updatepanel (content of is avaiable within page scope).
Below you can see fragment of control's source code:
public class Tab
{
[PersistenceMode(PersistenceMode.InnerProperty)]
[TemplateContainer(typeof(HtmlAnchorContainer))]
[DesignerSerializationVisibility(DesignerSerializationVisibility.Content)]
public ITemplate TabName { get; set; }
[PersistenceMode(PersistenceMode.InnerProperty)]
[TemplateContainer(typeof(PanelContainer))]
[DesignerSerializationVisibility(DesignerSerializationVisibility.Content)]
public ITemplate TabContainer { get; set; }
}
public class TabControl : System.Web.UI.WebControls.WebControl
{
List<Tab> tabs;
[DesignerSerializationVisibility(DesignerSerializationVisibility.Content),
PersistenceMode(PersistenceMode.InnerProperty),
NotifyParentProperty(true)]
public List<Tab> Tabs
{
get { return tabs ?? new List<Tab>(); }
set { tabs = value; }
}
protected override void CreateChildControls()
{
base.CreateChildControls();
.....
foreach (Tab tabItem in Tabs)
{
//generating tree control for further rendering
}
...
}
protected override void OnDataBinding(EventArgs e)
{
EnsureChildControls();
base.OnDataBinding(e);
}
}
Hope for you advices, recommendations, links and constructive criticism.))
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
将:
声明应用于您想要直接引用控件的模板,然后应该解决这个问题。
Apply a:
declaration to your templates where you want to reference the control directly, then this should be resolved.