我的ASP.NET项目中出现过这种类型的样板代码。
<div class="inputfield">
<div class="tl">
<span class="tr"><!-- --></span>
<span class="ll"><!-- --></span>
<div class="lr">
<div class="cntnt">
<asp:TextBox .../>
</div>
</div>
</div>
</div>
您可能已经猜到,除了最里面的文本字段外,该片段中的所有内容都是纯样板文件。
在ASP.NET中避免使用这种样板的最佳方法是什么?在例如Django我会为它制作一个自定义标签:
{% boiler %}
<input ... />
{% endboiler %}
我在想,也许我可以创建一个用户控件,但是我发现的ASP.NET用户控件的所有教程都非常简单和“自我关闭”,即他们不知道的内容标签。我需要的是:
<Hello:MyControl>
<asp:TextBox .../>
</Hello>
所以我的问题如下:避免样板的最佳方法是什么?
答案 0 :(得分:2)
您可以使用ITemplate属性。因此,您可以在不同的情况下注入不同的内容。
[PersistChildren(false), ParseChildren(true, "ContentTemplate")]
public partial class WebUserControl1 : System.Web.UI.UserControl
{
[System.ComponentModel.Browsable(false), System.Web.UI.PersistenceMode(PersistenceMode.InnerProperty)]
public ITemplate ContentTemplate { get; set; }
protected override void CreateChildControls()
{
if (this.ContentTemplate != null)
this.ContentTemplate.InstantiateIn(this);
base.CreateChildControls();
}
}
答案 1 :(得分:0)
将asp:TextBox与其他html标记一起放在用户控件中。在用户控件上提供与文本框属性匹配的属性,以便您执行以下操作:
<Hello:MyControl ID="myControl" runat="server" Width="300px" MaxLength="30" />
然后将width和maxlength属性传输到内部文本框。
您还可以从usercontrol提供对文本框的访问权限,并在后面的代码中设置所有属性。
答案 2 :(得分:0)
创建一个这样的类:
[PersistChildren(false), ParseChildren(true, "ContentTemplate")]
public class CustomContent:WebControl
{
[System.ComponentModel.Browsable(false), System.Web.UI.PersistenceMode(PersistenceMode.InnerDefaultProperty)]
public ITemplate ContentTemplate { get; set; }
private PlaceHolder m_placeHolder;
protected override void CreateChildControls()
{
m_placeHolder = new PlaceHolder();
if (this.ContentTemplate != null)
this.ContentTemplate.InstantiateIn(m_placeHolder);
Controls.Add(m_placeHolder);
}
protected override void RenderContents(HtmlTextWriter writer)
{
writer.Write(@"<div class=""inputfield"">
<div class=""tl"">
<span class=""tr""><!-- --></span>
<span class=""ll""><!-- --></span>
<div class=""lr"">
<div class=""cntnt"">
");
base.RenderContents(writer);
writer.Write(@" </div>
</div>
</div>
</div>
");
}
}
此类不是“用户控件”,而是“服务器控件”。您可以使用用户控件执行相同的操作,但设计人员会遇到问题。这将适用于设计师。 你可以在你的ASPX中加上这样的标记:
<uc1:CustomContent runat="server" ID="content">
<asp:textbox runat="server"></asp:textbox>
</uc1:CustomContent>
不要忘记aspx顶部的Register页面声明
<%@ Register tagprefix="uc1" Assembly="Assembly where CustomContent is" Namespace="namespace where CustomContent is" %>
你可以在uc1:CustomContent标签中放置你想要的任何东西,它会在它周围呈现那个样板文件html。如果您对ITemplate的工作原理感到好奇,那么很多关于msdn等的文章