LinqDataSource.ContextCreating Event


The .NET API Reference documentation has a new home. Visit the .NET API Browser on to see the new experience.

Occurs before an instance of the context type object is created.

Namespace:   System.Web.UI.WebControls
Assembly:  System.Web.Extensions (in System.Web.Extensions.dll)

public event EventHandler<LinqDataSourceContextEventArgs> ContextCreating

By default, the LinqDataSource control creates an instance of the type that is specified in the ContextTypeName property. The LinqDataSource control calls the default constructor of the data context object to create an instance of the object. It is possible that you have to use a non-default 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.

The following example shows how to create the data context object by using a non-default 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);

.NET Framework
Available since 3.5
Return to top