LinqDataSource.ContextCreating LinqDataSource.ContextCreating LinqDataSource.ContextCreating LinqDataSource.ContextCreating Event

Definition

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) 

Examples

The following example shows how to create the data context object by using a non-parameterless constructor during the ContextCreating event. 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

Remarks

By default, the LinqDataSource control creates an instance of the type that is specified in the ContextTypeName property. The LinqDataSource control calls the parameterless constructor of the data context object to create an instance of the object. It 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. In that case, you must handle the ContextCreating event and manually create the data context object.

The context type object is created during select, update, insert, and delete operations.

There is one time when the event is not raised. This is when you programmatically set the Result property to an object in an event handler for the Selecting event. 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.

Applies to