DynamicControl.DataField 屬性

定義

取得或設定 DynamicControl 控制項所繫結的資料欄位名稱。Gets or sets the name of the data field that the DynamicControl control is bound to.

public:
 property System::String ^ DataField { System::String ^ get(); void set(System::String ^ value); };
public string DataField { get; set; }
member this.DataField : string with get, set
Public Property DataField As String

屬性值

要繫結 DynamicControl 控制項的資料欄位名稱。The name of the data field to bind the DynamicControl control to. 預設為空字串 (""),表示這個屬性未設定。The default is an empty string (""), which indicates that this property is not set.

範例

下列範例示範如何使用DataField屬性來指定要DynamicControl在控制項中顯示的欄位。The following example demonstrates how to use the DataField property to specify which field to display in a DynamicControl control.

<%@ Page Language="C#" %>

<%@ Register Assembly="System.Web.Entity, Version=3.5.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" Namespace="System.Web.UI.WebControls" tagprefix="asp" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<script runat="server">
  protected void Page_Init(object sender, EventArgs e)
  {
      DynamicDataManager1.RegisterControl(ProductsFormView);
  }
</script>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
  <title>DynamicControl.DataField Sample</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
      <asp:DynamicDataManager ID="DynamicDataManager1" runat="server"
        AutoLoadForeignKeys="true" />
        
      <asp:FormView ID="ProductsFormView" runat="server" DataSourceID="ProductsDataSource" 
        DataKeyNames="ProductID" AllowPaging="True" 
        PagerSettings-PageButtonCount="15">
        <ItemTemplate>
          Name:
          <asp:DynamicControl runat="server" DataField="Name" />
          <br />
          Product Number:
          <asp:DynamicControl runat="server" DataField="ProductNumber" />
          <br />
          Product Category:
          <asp:DynamicControl runat="server" DataField="ProductCategory" />
          <br />
        </ItemTemplate>
      </asp:FormView>
      
      <!-- This example uses Microsoft SQL Server and connects   -->
      <!-- to the AdventureWorksLT sample database.              -->      
      <asp:EntityDataSource ID="ProductsDataSource" runat="server"
        ContextTypeName="AdventureWorksLT_DataModel.AdventureWorksLT_DataEntities"
        EntitySetName="Product"
        Where='it.Size="L"'>
      </asp:EntityDataSource>
      
    </div>
    </form>
</body>
</html>
<%@ Page Language="VB" %>

<%@ Register Assembly="System.Web.Entity, Version=3.5.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" Namespace="System.Web.UI.WebControls" tagprefix="asp" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<script runat="server">
    
    Protected Sub Page_Init(ByVal sender As Object, ByVal e As System.EventArgs)
        DynamicDataManager1.RegisterControl(ProductsFormView)
    End Sub
    
</script>

<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
  <title>DynamicControl.DataField Sample</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
      <asp:DynamicDataManager ID="DynamicDataManager1" runat="server"
        AutoLoadForeignKeys="true" />
        
      <asp:FormView ID="ProductsFormView" runat="server" DataSourceID="ProductsDataSource" 
        DataKeyNames="ProductID" AllowPaging="True" 
        PagerSettings-PageButtonCount="15">
        <ItemTemplate>
          Name:
          <asp:DynamicControl runat="server" DataField="Name" />
          <br />
          Product Number:
          <asp:DynamicControl runat="server" DataField="ProductNumber" />
          <br />
          Product Category:
          <asp:DynamicControl runat="server" DataField="ProductCategory" />
          <br />
        </ItemTemplate>
      </asp:FormView>

      <!-- This example uses Microsoft SQL Server and connects   -->
      <!-- to the AdventureWorksLT sample database.              -->
      <asp:EntityDataSource ID="ProductsDataSource" runat="server"
        ContextTypeName="AdventureWorksLT_DataModel.AdventureWorksLT_DataEntities"
        EntitySetName="Product"
        Where='it.Size="L"'>
      </asp:EntityDataSource>
      
    </div>
    </form>
</body>
</html>

備註

使用屬性, 即可指定要系結DynamicControl至控制項的資料欄位。 DataFieldUse the DataField property to specify the data field to bind to the DynamicControl control. 資料欄位可以是資料行名稱, 或者, 對於外鍵資料行而言, 則是關聯性名稱。The data field can be the column name or, for foreign key columns, the relationship name.

根據預設, DynamicControl控制項會使用與欄位資料類型相關聯的欄位範本來顯示指定的資料欄位。By default, the specified data field is displayed by the DynamicControl control using the field template that is associated with the field data type. 您可以藉由設定UIHint屬性來變更針對資料欄位呈現的欄位範本。You can change the field template that is rendered for the data field by setting the UIHint property. 您也可以藉由設定DataFormatString屬性來格式化顯示的文字。You can also format the displayed text by setting the DataFormatString property.

注意如果您想要全域變更資料欄位的顯示和行為, 則必須將屬性套用至您的資料模型, 而不是在DynamicControl控制項的特定實例中設定此項。Note If you want to change the display and behavior of a data field globally, you must apply attributes to your data model instead of setting this in a particular instance of the DynamicControl control. 如需詳細資訊, 請參閱ASP.NET 動態資料For more information, see ASP.NET Dynamic Data.

適用於

另請參閱