DynamicDataExtensions.FindFieldTemplate(Control, String) 方法

定義

傳回在指定之控制項的命名容器中所指定資料行的欄位樣板。

public:
[System::Runtime::CompilerServices::Extension]
 static System::Web::UI::Control ^ FindFieldTemplate(System::Web::UI::Control ^ control, System::String ^ columnName);
public static System.Web.UI.Control FindFieldTemplate (this System.Web.UI.Control control, string columnName);
static member FindFieldTemplate : System.Web.UI.Control * string -> System.Web.UI.Control
<Extension()>
Public Function FindFieldTemplate (control As Control, columnName As String) As Control

參數

control
Control

內含控制項。

columnName
String

資料行名稱。

傳回

指定之控制項的命名容器中所指定資料行的欄位樣板。

範例

下列範例示範如何尋找指定之資料行名稱的文字方塊控制項。 例如,動態資料頁面範本會複製到 \DynamicData\CustomPages\ProductDescriptions 資料夾,以提供 AdventureWorksLT 資料庫的 ProductDescription 資料表的自訂顯示。 Insert.aspx 檔案標記已變更,以新增 OnDataBound 名為 DetailsView1_DataBound 的事件處理常式,其中 FindDataSourceControl 會呼叫 方法來尋找文字方塊控制項。

using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Xml.Linq;
using System.Web.DynamicData;

public partial class Insert : System.Web.UI.Page {
    protected MetaTable table;

    protected void Page_Init(object sender, EventArgs e) {
        DynamicDataManager1.RegisterControl(DetailsView1);
    }

    protected void Page_Load(object sender, EventArgs e) {
        table = DetailsDataSource.GetTable();
        Title = table.DisplayName;
    }

    protected void DetailsView1_DataBound(object sender, EventArgs e) {

        var dsc = DetailsView1.FindDataSourceControl() as LinqDataSource;
        if (dsc == null || dsc.EnableInsert != true)
            return;

        var mTbl = DetailsView1.FindMetaTable() as MetaTable;
        if (mTbl != null)
            LblMetaTbl.Text = "Column count = " + mTbl.Columns.Count.ToString();

        var fldTmpUsrCtl = DetailsView1.FindFieldTemplate("Description") as FieldTemplateUserControl;
        if (fldTmpUsrCtl != null) {
            var entryFldDescript = fldTmpUsrCtl.DataControl as TextBox;
            entryFldDescript.Text = "(Enter short Description here.)";
        }

        var fldTmpUsrCtl2 = DetailsView1.FindFieldTemplate("ModifiedDate") as FieldTemplateUserControl;
        if (fldTmpUsrCtl2 != null) {
            var entryFldModDate = fldTmpUsrCtl2.DataControl as TextBox;
            entryFldModDate.Text = System.DateTime.Now.Date.ToShortDateString();
        }
    }

    protected void DetailsView1_ItemCommand(object sender, DetailsViewCommandEventArgs e) {
        if (e.CommandName == DataControlCommands.CancelCommandName) {
            Response.Redirect(table.ListActionPath);
        }
    }

    protected void DetailsView1_ItemInserted(object sender, DetailsViewInsertedEventArgs e) {
        if (e.Exception == null) {
            Response.Redirect(table.ListActionPath);
        }
    }
}
<%@ Page Language="C#" MasterPageFile="~/Site.master" CodeFile="Insert.aspx.cs" Inherits="Insert" %>

<asp:Content ID="Content1" ContentPlaceHolderID="ContentPlaceHolder1" Runat="Server">
    <asp:DynamicDataManager ID="DynamicDataManager1" runat="server" AutoLoadForeignKeys="true" />

    <h2> DynamicData\CustomPages\ProductDescriptions\Insert.aspx <%= table.DisplayName %></h2>
    <p>   <asp:Label ID="LblMetaTbl" runat="server" Text="Label"></asp:Label>   </p>

    <asp:ScriptManagerProxy runat="server" ID="ScriptManagerProxy1" />

    <asp:ValidationSummary ID="ValidationSummary1" runat="server" EnableClientScript="true"
        HeaderText="List of validation errors" />
    <asp:DynamicValidator runat="server" ID="DetailsViewValidator" ControlToValidate="DetailsView1" Display="None" />

    <asp:DetailsView ID="DetailsView1" runat="server" DataSourceID="DetailsDataSource" DefaultMode="Insert"
        AutoGenerateInsertButton="True" OnItemCommand="DetailsView1_ItemCommand" OnItemInserted="DetailsView1_ItemInserted"
        CssClass="detailstable" FieldHeaderStyle-CssClass="bold"
        OnDataBound="DetailsView1_DataBound">
    </asp:DetailsView>

    <asp:LinqDataSource ID="DetailsDataSource" runat="server" EnableInsert="true">
    </asp:LinqDataSource>
</asp:Content>

適用於