1

我在我的 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>

所以我的问题如下:避免样板的最佳方法是什么?

4

3 回答 3

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();
    }
}
于 2009-10-30T15:46:58.000 回答
0

将 asp:TextBox 与其他 html 标记一起放在您的用户控件中。在您的用户控件上提供与文本框的属性相匹配的属性,以便您执行以下操作:

<Hello:MyControl ID="myControl" runat="server" Width="300px" MaxLength="30" />

然后 width 和 maxlength 属性将被转移到内部文本框。

您还可以从用户控件提供对文本框的访问权限,并在后面的代码中设置所有属性。

于 2009-10-30T15:38:27.610 回答
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 tagprefix="uc1" Assembly="Assembly where CustomContent is" Namespace="namespace where CustomContent is" %>

您可以在 uc1:CustomContent 标记中放置任何您想要的内容,它会在其周围呈现样板 html。如果你对 ITemplate 的工作原理感到好奇,msdn 等上有很多文章。

于 2009-11-05T16:49:14.247 回答