This documentation is archived and is not being maintained.

LinqDataSource.ContextCreating Event

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
<asp:LinqDataSource OnContextCreating="EventHandler" />

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);

Windows 7, Windows Vista, Windows XP SP2, Windows Server 2008 R2, Windows Server 2008, Windows Server 2003

The .NET Framework and .NET Compact Framework do not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.

.NET Framework

Supported in: 3.5