GridView 列字段的基类DataControlField 类

用作所有数据控件字段类型的基类,这些类型表示如 DetailsViewGridView 等表格数据绑定控件中的数据列。

命名空间:System.Web.UI.WebControls
程序集:System.Web(在 system.web.dll 中)

public abstract class DataControlField : IStateManager, IDataSourceViewSchemaAccessor

 

DataControlField 类用作所有数据控件字段类型的基类。数据绑定控件使用数据控件字段表示数据字段,类似于 DataGridColumn 对象表示 DataGrid 控件中的列的类型的方式。

使用从 DataControlField 派生的类来控制如 DetailsViewGridView 等数据绑定控件中的数据字段的显示方式。

列字段类型

说明

BoundField

以文本形式显示数据源中某个字段的值。

ButtonField

显示数据绑定控件中的命令按钮。根据控件的不同,这允许您显示带有自定义按钮控件的行或列,如“添加”或“移除”按钮。

CheckBoxField

显示数据绑定控件中的复选框。此数据控件字段类型通常用于显示带有布尔值的字段。

CommandField

显示数据绑定控件中要执行编辑、插入或删除操作的内置命令按钮。

HyperLinkField

将数据源中某个字段的值显示为超链接。此数据控件字段类型允许您将第二个字段绑定到超链接的 URL。

ImageField

显示数据绑定控件中的图像。

TemplateField

根据指定的模板,显示数据绑定控件中的用户定义内容。

也可以扩展 DataControlFieldBoundField 类来创建自己的数据控件字段类型

DataControlField 类提供许多属性,这些属性确定用户界面 (UI) 元素如何存在于数据绑定控件中。在呈现 UI 时,并非每个控件都使用所有可用的数据控件字段属性。例如,将数据控件字段显示为行的 DetailsView 控件包含每个数据控件字段的标题项,但没有脚注项。因此,DetailsView 控件将忽略 FooterTextFooterStyle 属性。然而,如果 ShowFooter 属性设置为 true,则 GridView 控件使用 FooterTextFooterStyle 属性。同样地,数据控件字段属性将根据元素的不同对 UI 元素的表示产生影响。ItemStyle 属性始终应用于该字段。如果从 DataControlField 派生的类型包含某个控件,如同在 ButtonFieldCheckBoxField 类中,则 ControlStyle 属性将应用于该字段。

下面的代码示例演示如何使用从 DataControlField 派生的 BoundFieldButtonField 对象来显示 DetailsView 控件中的行。DetailsView 控件将 AutoGenerateRows 属性设置为 false这使它能够显示由 SelectCommand 属性返回的数据子集。

 

<%@ page language="C#" %>
<html>
<body>
  <form runat="server">

    <asp:sqldatasource
      id="SqlDataSource1"
      runat="server"
      connectionstring="<%$ ConnectionStrings:MyNorthwind%>"
      selectcommand="Select * From Employees">
    </asp:sqldatasource>

    <asp:detailsview
      id="DetailsView1"
      runat="server"
      allowpaging="True"
      datasourceid="SqlDataSource1"
      height="208px"
      width="264px"
      autogeneraterows="False">
        <fields>

         <asp:boundfield
            sortexpression="LastName"
            datafield="LastName"
            headertext="LastName">
              <itemstyle backcolor="Yellow">
              </itemstyle>
          </asp:boundfield>
         

         <asp:boundfield
            sortexpression="FirstName"
            datafield="FirstName"
            headertext="FirstName">
              <itemstyle forecolor="#C00000">
              </itemstyle>
          </asp:boundfield>

          <asp:buttonfield
            text="TestButton"
            buttontype="Button">
          </asp:buttonfield>
      

  </fields>
    </asp:detailsview>

  </form>
</body>
</html>

 

下面的代码示例演示如何扩展 BoundField 类来创建可用于 GridView 控件的自定义绑定字段。与 CheckBoxField 类相似,RadioButtonField 类表示 truefalse 数据的列。然而,尽管 CheckBoxField 类绑定到的数据可以是任何 truefalse 值的集,但 RadioButtonField 类绑定到的数据集在任何给定的时间只能有一个 true 值。此示例演示如何实现 ExtractValuesFromCellInitializeCell 方法,它们是从 DataControlField 派生的所有类的两种重要的方法。

 

 

namespace Samples.AspNet.CS {

  using System;
  using System.Collections;
  using System.Collections.Specialized;
  using System.ComponentModel;
  using System.Security.Permissions;
  using System.Web;
  using System.Web.UI;
  using System.Web.UI.WebControls;

  [AspNetHostingPermission(SecurityAction.Demand,
      Level=AspNetHostingPermissionLevel.Minimal)]
  public sealed class RadioButtonField : CheckBoxField {

    public RadioButtonField() {
    }

    // Gets a default value for a basic design-time experience.
    // Since it would look odd, even at design time, to have
    // more than one radio button selected, make sure that none
    // are selected.
    protected override object GetDesignTimeValue() {
        return false;
    }
    // This method is called by the ExtractRowValues methods of
    // GridView and DetailsView. Retrieve the current value of the
    // cell from the Checked state of the Radio button.
    public override void ExtractValuesFromCell(IOrderedDictionary dictionary,
                                               DataControlFieldCell cell,
                                               DataControlRowState rowState,
                                               bool includeReadOnly)
    {

      // Determine whether the cell contains a RadioButton
      // in its Controls collection.
      if (cell.Controls.Count > 0) {
        RadioButton radio = cell.Controls[0] as RadioButton;

        object checkedValue = null;
        if (null == radio) {
          // A RadioButton is expected, but a null is encountered.
          // Add error handling.
          throw new InvalidOperationException
              ("RadioButtonField could not extract control.");
        }
        else {
            checkedValue = radio.Checked;
        }


        // Add the value of the Checked attribute of the
        // RadioButton to the dictionary.
        if (dictionary.Contains(DataField))
          dictionary[DataField] = checkedValue;
        else
          dictionary.Add(DataField, checkedValue);
      }
    }
    // This method adds a RadioButton control and any other
    // content to the cell's Controls collection.
    protected override void InitializeDataCell
        (DataControlFieldCell cell, DataControlRowState rowState) {

      RadioButton radio = new RadioButton();

      // If the RadioButton is bound to a DataField, add
      // the OnDataBindingField method event handler to the
      // DataBinding event.
      if (DataField.Length != 0) {
        radio.DataBinding += new EventHandler(this.OnDataBindField);
      }

      radio.Text = this.Text;

      // Because the RadioButtonField is a BoundField, it only
      // displays data. Therefore, unless the row is in edit mode,
      // the RadioButton is displayed as disabled.
      radio.Enabled = false;
      // If the row is in edit mode, enable the button.
      if ((rowState & DataControlRowState.Edit) != 0 ||
          (rowState & DataControlRowState.Insert) != 0) {
        radio.Enabled = true;
      }

      cell.Controls.Add(radio);
    }
  }
}

下面的代码示例演示如何在 GridView 控件中使用前一示例中提供的 RadioButtonField 类。在此示例中,GridView 控件显示一支运动队的数据。运动员数据在一个数据表中进行维护,此表包括 ID 列、运动员姓名的列以及标识该队队长的 true 或 false 列。RadioButtonField 类用于显示哪位队员目前是队长。可编辑 GridView 控件来选择新的队长或更改其他队员信息。

 

<%@ page language="C#" %>
<%@ Register Tagprefix="aspSample"
             Namespace="Samples.AspNet.CS"
             Assembly="Samples.AspNet.CS" %>

<html>
<body>
    <form runat="server">
        <asp:gridview
          id="GridView1"
          runat="server"
          allowpaging="True"
          datasourceid="SqlDataSource1"
          allowsorting="True"
          autogeneratecolumns="False"
          autogenerateeditbutton="True"
          datakeynames="AnID">
            <columns>

                <aspSample:radiobuttonfield
                  headertext="RadioButtonField"
                  text="TeamLeader"
                  datafield="TrueFalse">
                </aspSample:radiobuttonfield>

                <asp:boundfield
                  insertvisible="False"
                  sortexpression="AnID"
                  datafield="AnID"
                  readonly="True"
                  headertext="AnID">
                </asp:boundfield>

                <asp:boundfield
                  sortexpression="FirstName"
                  datafield="FirstName"
                  headertext="FirstName">
                </asp:boundfield>

                <asp:boundfield
                  sortexpression="LastName"
                  datafield="LastName"
                  headertext="LastName">
                </asp:boundfield>

              </columns>
        </asp:gridview>
        <asp:sqldatasource
          id="SqlDataSource1"
          runat="server"
          connectionstring="<%$ ConnectionStrings:MyNorthwind%>"
          selectcommand="SELECT AnID,FirstName,LastName,TeamLeader FROM Players"
          updatecommand="UPDATE Players SET TrueFalse='false';UPDATE Players SET TrueFalse='true' WHERE AnID=@anID">

        </asp:sqldatasource>

    </form>
</body>
</html>

 

 

 

posted @ 2009-04-10 23:59  minmin8110  阅读(846)  评论(0编辑  收藏  举报