LinqDataSource.Selecting Event


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

Occurs before a data-retrieval operation.

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

Public Event Selecting As EventHandler(Of LinqDataSourceSelectEventArgs)

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.

Partial Class Default3
    Inherits System.Web.UI.Page

    Dim citiesArray() As String = _
    { _
        "Atlanta", _
        "Charlotte", _
        "Denver", _
        "New York", _
        "San Francisco" _

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

    End Sub

    Protected Sub LinqDataSource_Selecting(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.LinqDataSourceSelectEventArgs) Handles LinqDataSource1.Selecting
        Dim cities = From city In citiesArray _
                     Where city > "B" _
                     Select city
        e.Result = cities
        ' Or we could set e.Result = citiesArray to return all rows.
    End Sub

End Class

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
Available since 3.5
Return to top