This documentation is archived and is not being maintained.

LinqDataSource.Selecting Event

Occurs before a data-retrieval operation.

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

public event EventHandler<LinqDataSourceSelectEventArgs> Selecting
<asp:LinqDataSource OnSelecting="EventHandler" />

Handle the Selecting event in order to perform the following tasks:

  • Modify parameters for data retrieval.

  • Generate the query programmatically.

  • Modify the values for sorting or paging.

  • Perform custom sorting or paging.

  • Cancel the data-retrieval operation.

The LinqDataSourceSelectEventArgs object that is passed to event handlers for this event contains the parameters for the data-retrieval operation. You can modify the parameters in the Selecting event handler before the query executes, or you can create a new result set and assign that to the Result property.

Your ability to implement custom sorting or paging in handlers for this event might be limited by the control that is bound to the LinqDataSource. For example, when the column header of a GridView control is clicked, the control performs automatic sorting which might override whatever order you establish in the event handler.

If an exception is thrown in an event handler for the Selecting event, you must handle the exception in that event handler. The exception will not be passed to an event handler for the Selected event (through the Exception property of the LinqDataSourceStatusEventArgs object). The Exception property contains only the exceptions that are thrown after the Selecting event.

The following example shows an event handler for the Selecting event. The handler creates a query that retrieves values from an array of string values in the Web page.

public partial class Default3 : System.Web.UI.Page
    string[] citiesArray = 
        "New York", 
        "San Francisco" 

    protected void Page_Load(object sender, EventArgs e)

    protected void LinqDataSource_Selecting(object sender, LinqDataSourceSelectEventArgs e)
        var cities = from city in citiesArray
                     where city.CompareTo("B") > 0
                     select city;
        e.Result = cities;
        // Or we could set e.Result = citiesArray to return all rows.

The following example shows how to assign the Result property to the object that is returned from a method that represents a stored procedure.

[Visual Basic]

Protected Sub LinqDataSource_Selecting(ByVal sender As Object, _
        ByVal e As LinqDataSourceSelectEventArgs)
    Dim exampleContext As ExampleDataContext = New ExampleDataContext()
    e.Result = exampleContext.GetRegisteredCustomers()
End Sub


protected void LinqDataSource_Selecting(object sender, 
        LinqDataSourceSelectEventArgs e)
    ExampleDataContext exampleContext = new ExampleDataContext();
    e.Result = exampleContext.GetRegisteredCustomers();

.NET Framework

Supported in: 4, 3.5

Windows 7, Windows Vista SP1 or later, Windows XP SP3, Windows Server 2008 (Server Core not supported), Windows Server 2008 R2 (Server Core supported with SP1 or later), Windows Server 2003 SP2

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