ASP.NET控件开发基础(16)

http://tech.ddvip.com   2007年09月02日    社区交流

内容摘要:ASP.NET控件开发基础

  OK,你可以看一下效果了,当然你可以定义多个模板然后在多个不同模板内添加内容.我们来看下其控件树内容,如下图

  ASP.NET控件开发基础(16)

  子控件有一个Label控件,非控件内容则以LiteralControl呈现.

  2.2实现动态模板

  当我们使用DataList控件时,往往在模板中动态的绑定一些数据,获取的这些数据则是ITemplate接口的InstantiateIn 方法中的容器控件.下面我们为控件定义属性,然后通过DataBind()方法和数据绑定表达式获取数据

  我们先先定义三个属性

  页面代码,注意要用DataBind()方法

  void Page_Load()
  {
    Article1.Title = "Creating Templated Databound Controls";
    Article1.Author = "Stephen Walther";
    Article1.Contents = "Blah, blah, blah, blah";
    Article1.DataBind();
  }

  通过Container数据绑定表达式获取容器对象属性,此处容器对象为默认的Article

  ASP.NET控件开发基础(16)

  如下实现

  <custom:Article
    id="Article1"
    Runat="server">
    <ItemTemplate>
    <asp:Label ID="Label1" runat="server" Text="Label"></asp:Label>
    <%# Container.Title%><br />
    <%# Container.Author %><br />
    <%# Container.Contents %><br />
    </ItemTemplate>
  </custom:Article>

责编:豆豆技术应用

正在加载评论...