LinqDataSource.ContextTypeName 属性
定义
获取或设置包含属性(其值包含要检索的数据)的类型的名称。Gets or sets the name of the type that contains the property whose value has the data that you want to retrieve.
public:
property System::String ^ ContextTypeName { System::String ^ get(); void set(System::String ^ value); };
public:
virtual property System::String ^ ContextTypeName { System::String ^ get(); void set(System::String ^ value); };
public string ContextTypeName { get; set; }
public override string ContextTypeName { get; set; }
member this.ContextTypeName : string with get, set
Public Property ContextTypeName As String
Public Overrides Property ContextTypeName As String
属性值
要从中检索数据的类的名称。The name of the class to retrieve data from.
示例
下面的示例演示如何将属性设置 ContextTypeName 为包含字符串数组的类。The following example shows how to set the ContextTypeName property to a class that contains an array of strings. 它还演示了如何将属性设置为 () 表示数据库的 O/R 设计器生成的类。It also shows how to set the property to a class (generated by the O/R Designer) that represents a database.
<!-- Retrieve and display data from array of string values -->
<asp:LinqDataSource
ContextTypeName="MovieLibrary"
TableName="AvailableGenres"
ID="LinqDataSource1"
runat="server">
</asp:LinqDataSource>
<asp:DropDownList
DataSourceID="LinqDataSource1"
runat="server"
ID="DropDownList1">
</asp:DropDownList>
<!-- Retrieve and display data from database -->
<asp:LinqDataSource
ContextTypeName="ExampleDataContext"
TableName="Movies"
Select="Title"
ID="LinqDataSource2"
runat="server">
</asp:LinqDataSource>
<asp:DropDownList
DataSourceID="LinqDataSource2"
runat="server"
ID="DropDownList2">
</asp:DropDownList>
<!-- Retrieve and display data from array of string values -->
<asp:LinqDataSource
ContextTypeName="MovieLibrary"
TableName="AvailableGenres"
ID="LinqDataSource1"
runat="server">
</asp:LinqDataSource>
<asp:DropDownList
DataSourceID="LinqDataSource1"
runat="server"
ID="DropDownList1">
</asp:DropDownList>
<!-- Retrieve and display data from database -->
<asp:LinqDataSource
ContextTypeName="ExampleDataContext"
TableName="Movies"
Select="Title"
ID="LinqDataSource2"
runat="server">
</asp:LinqDataSource>
<asp:DropDownList
DataSourceID="LinqDataSource2"
runat="server"
ID="DropDownList2">
</asp:DropDownList>
ExampleDataContext在此示例中不显示名为的类,它表示数据库表。The class named ExampleDataContext that represents the database table is not shown in this example. 要使此示例正常运行,必须通过添加一个名为 Example 的 LINQ To SQL 类并将名为 "Movie" 的表拖到 O/R 设计器上来创建此类。For this example to work, you must create this class by adding a LINQ To SQL class named Example.dbml and dragging a table named Movie onto the O/R Designer. 生成一个名为的类 ExampleDataContext ,其中包含名为的属性 Movies 。A class named ExampleDataContext with a property named Movies is generated.
下面的示例显示了 MovieLibrary 在控件中引用的名为的类 LinqDataSource 。The following example shows the class named MovieLibrary that is referenced in the LinqDataSource control.
public class MovieLibrary
{
string[] _availableGenres = { "Comedy", "Drama", "Romance" };
public MovieLibrary()
{
}
public string[] AvailableGenres
{
get
{
return _availableGenres;
}
}
}
Public Class MovieLibrary
Dim _availableGenres() As String = {"Comedy", "Drama", "Romance"}
Public ReadOnly Property AvailableGenres() As String()
Get
Return _availableGenres
End Get
End Property
End Class
注解
使用 LinqDataSource 控件从内存中数据集合或数据库检索数据时,必须指定两个属性。When you use the LinqDataSource control to retrieve data from either an in-memory data collection or a database, you must specify two properties. 第一个是表示数据源的数据上下文类。The first is a data context class that represents the data source. 第二个是数据上下文类中包含数据的属性。The second is a property in the data context class that contains the data. 将属性设置 ContextTypeName 为数据上下文类的名称,并将 TableName 属性设置为包含数据的数据集合。You set the ContextTypeName property to the name of the data context class and you set the TableName property to the data collection that contains the data.
例如,从数据库中检索数据时,请将属性设置 ContextTypeName 为表示数据库的类的名称。For example, when you retrieve data from a database, set the ContextTypeName property to the name of the class that represents the database. 还将 TableName 属性设置为表示数据库中的表的属性。Also set the TableName property to the property that represents the table in the database. 若要从数据库生成类,请使用 O/R 设计器或 SqlMetal.exe 实用程序来自动生成这些类。To generate classes from a database, use the O/R Designer or the SqlMetal.exe utility to automatically generate those classes.
从内存中的数据集合(如数组)中检索数据时,请将 ContextTypeName 属性设置为包含数组属性的类的名称。When you retrieve data from an in-memory data collection such as an array, set the ContextTypeName property to the name of the class that contains the array property. 然后将 TableName 属性设置为可获取数组的属性。Then set the TableName property to the property that gets the array.
若要通过控件启用自动更新、插入或删除操作 LinqDataSource ,则分配给属性的类 ContextTypeName 必须派生自 DataContext 。To enable automatic update, insert, or delete operations through the LinqDataSource control, the class assigned to the ContextTypeName property must derive from DataContext. 此外,分配给属性的属性 TableName 必须派生自 Table<TEntity> 。In addition, the property assigned to the TableName property must derive from Table<TEntity>. 如果不需要启用自动更新、插入或删除操作,则可以将任何类型的类的名称分配给 ContextTypeName 属性。If you do not need to enable automatic update, insert, or delete operations, you can assign the name of any type of class to the ContextTypeName property.
有关如何从类的实例中选择数据的信息,请参见 Result 属性。For information about how to select data from an instance of a class, see the Result property.