LinqDataSource.ContextCreating 事件

定义

在创建上下文类型对象实例前发生。Occurs before an instance of the context type object is created.

public:
 event EventHandler<System::Web::UI::WebControls::LinqDataSourceContextEventArgs ^> ^ ContextCreating;
public event EventHandler<System.Web.UI.WebControls.LinqDataSourceContextEventArgs> ContextCreating;
member this.ContextCreating : EventHandler<System.Web.UI.WebControls.LinqDataSourceContextEventArgs> 
Public Custom Event ContextCreating As EventHandler(Of LinqDataSourceContextEventArgs) 

事件类型

EventHandler<LinqDataSourceContextEventArgs>

示例

下面的示例演示如何通过在事件期间使用非参数构造函数来创建数据上下文对象 ContextCreatingThe following example shows how to create the data context object by using a non-parameterless constructor during the ContextCreating event. 该代码将对象分配给 ObjectInstance 属性。The code assigns the object to the ObjectInstance property.

protected void LinqDataSource_ContextCreating(object sender, LinqDataSourceContextEventArgs e)
{
    e.ObjectInstance = new ExampleDataContext(ConfigurationManager.ConnectionStrings["ExampleConnectionString"].ConnectionString);
}
Protected Sub LinqDataSource_ContextCreating(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.LinqDataSourceContextEventArgs)
    e.ObjectInstance = New ExampleDataContext(ConfigurationManager.ConnectionStrings("ExampleConnectionString").ConnectionString)
End Sub

注解

默认情况下,该 LinqDataSource 控件创建在属性中指定的类型的实例 ContextTypeNameBy default, the LinqDataSource control creates an instance of the type that is specified in the ContextTypeName property. LinqDataSource控件调用数据上下文对象的无参数构造函数来创建对象的实例。The LinqDataSource control calls the parameterless constructor of the data context object to create an instance of the object. 您可能必须使用非参数构造函数,或者必须创建一个与属性中指定的对象不同的对象 ContextTypeNameIt is possible that you have to use a non-parameterless constructor or you have to create an object that differs from the one specified in the ContextTypeName property. 在这种情况下,必须处理 ContextCreating 事件并手动创建数据上下文对象。In that case, you must handle the ContextCreating event and manually create the data context object.

上下文类型对象是在 select、update、insert 和 delete 操作过程中创建的。The context type object is created during select, update, insert, and delete operations.

不引发事件时有一次。There is one time when the event is not raised. 这是以编程方式将 Result 属性设置为事件的事件处理程序中的对象时 SelectingThis is when you programmatically set the Result property to an object in an event handler for the Selecting event. 另一种情况是,原始值不必存储在视图状态中,或者属性中的对象 Result 实现 ITable 接口。An additional condition is that either the original values do not have to be stored in view state, or the object in the Result property implements the ITable interface.

适用于