ComponentArt.web.ui中文帮助之Grid(三)

使用ComponentArt Grid的服务器端模板

通过它的不同的用户个性化定制特征,ComponentArt Grid包括服务器控件对内部内容使用服务器模板的正常能力,这篇文章概述了表格使用服务器端模板的典型用法,同时也指出了开发者可以获得的这些功能支持的事件和方法

在选择使用服务器段模板之前,必须保证读过一遍Web.UI控件模板概述。可能客户端模板更适合你的程序

概述

ComponentArt Grid中的服务器端模板专门用于模板化数据元素。他们允许任何服务器端元素绑定到表格的数据。最主要的,它使表格元素能够包含服务器端控件的能力,能够产生服务器端事件,保持状态和执行其他服务器端逻辑

定义服务器端模板

在服务器端模板能够用于表格之前,我们必须在它的ServerTemplates块中定义

  <ServerTemplates>

    ...

    <ComponentArt:GridServerTemplate ID="LabelTemplate" />

      <Template>

        <asp:Label runat="server" ID="myLabel" BorderWidth="2" Text="<%# Container.DataItem["FullName"] %>" />

      </Template>

    </ComponentArt:GridServerTemplate>

  </ServerTemplates>

上例会在调用此模板的元素中呈现一个有两像素边框、包含fullname字段的标签

要连接定义的模板到表格列,只要简单的设定GridColumnDataCellServerTemplateId到你要使用的服务器模板的ID

  <ComponentArt:GridColumn ... DataCellServerTemplateId="LabelTemplate" />

注意:数据绑定表达式的语法与你可能用过的语法有一些不同。不需要使用DataBinder.Eval来得到表达式。Container.DataItem总是指向模板绑定的GridItem (数据行),你可以简单的使用GridItem的字符串索引来引用行内部的字段

使用ItemDataBound事件

ComponentArt Grid具有对被绑定在一个GridItem上的服务器模板起作用的能力.这个事件是 ItemDataBound事件,它的参数包括模板示例的内容和所包含在的GridItem ,这给了开发者一个机会来在页面被呈现前操纵内部

下面是一个典型的用在Grid中的ItemDataBound实例

1.为列设置服务器端模板

  <ComponentArt:GridColumn ... DataCellServerTemplateId="myTemplate" />

定义你要用的模板

  <ServerTemplates>

    ...

    <ComponentArt:GridServerTemplate ID="myTemplate" />

      <Template>

        Here is a button:<br>

        <asp:Button ID="myButton" runat="server"

          Text="<%# Container.DataItem["Name"] %>" />

      </Template>

    </ComponentArt:GridServerTemplate>

  </ServerTemplates>

后置代码中的添加委托 :

  private void InitializeComponent()

  {

    ...

    Grid1.ItemDataBound += new Grid.ItemDataBoundEventHandler(Grid1_ItemDataBound);

  }

定义处理程序:

  private void Grid1_ItemDataBound(object sender, GridItemDataBoundEventArgs args)

  {

    // We can modify the content...

    args.Content.Controls.Add(new LiteralControl("Manipulated!"));

    // reference individual control instances...

    Button myButton = (Button)args.Content.FindControl("myButton");

    // or perform other logic based on the instantiated content.

    Response.Write("Instantiated " + myButton.UniqueID +

      " for item " + args.Item["Name"];

  }

使用ItemCommand事件

ComponentArt Grid能够响应植入在内部的服务器模板产生的事件,这个事件就是 ItemCommand, 它的参数包括引发事件的控件和模板所属的行(GridItem).

下面是一个典型的用在Grid中的ItemCommand实例

1.为列设置放置可以触发事件的控件的服务器端模板

  <ComponentArt:GridColumn ... DataCellServerTemplateId="linkButtonTemplate" />

定义你要用的模板:

  <ServerTemplates>

    ...

    <ComponentArt:GridServerTemplate ID="linkButtonTemplate" />

      <Template>

        <asp:LinkButton ID="lb" runat="server"

          Text="ItemCommand" CommandName="MyCommand" />

      </Template>

    </ComponentArt:GridServerTemplate>

  </ServerTemplates>

添加事件委托到后置代码

  private void InitializeComponent()

  {

    ...

    Grid1.ItemCommand += new Grid.ItemCommandEventHandler(Grid1_ItemCommand);

  }

定义事件处理程序:

  public void Grid1_ItemCommand(object sender, GridItemCommandEventArgs args)

  {

    Response.Write("Command " + ((LinkButton)args.Control).CommandName +

      " issued on item " + args.Item["ID"]);

  }

获得模板实例内部控件的引用

为获得模板实例内部控件的引用,表格控件提供了一个对FindControl方法的重写. Grid.FindControl能够取得4个参数:层级编号(level index),列编号,行编号,和空间ID.这个调用能够通过给定的层级,,,元素ID找到控件

例如,下面的调用能够找到第一层第三列第七行的模板内的控件"myButton"

  Button myButton = (Button)Grid.FindControl(0, 3, 7, "myButton");

为了更好的使用FindControl重写,我们可以使用GridColumnCollection GridItemCollectionIndexOf方法.这样,我们可以通过列的DataField和与行匹配的值找回行号和列号

  int columnIndex = Grid.Levels[0].Columns.IndexOf("Summary");

  int itemIndex = Grid.Items.IndexOf("UniqueID", 3984);

   

  Button myButton = (Button)Grid.FindControl(0, columnIndex, itemIndex, "myButton");

原文地址:https://www.cnblogs.com/liujuncm5/p/973031.html