Export (0) Print
Expand All

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 = 
    { 
        "Atlanta", 
        "Charlotte", 
        "Denver", 
        "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

[C#]

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

.NET Framework

Supported in: 4.5.2, 4.5.1, 4.5, 4, 3.5

Windows 8.1, Windows Server 2012 R2, Windows 8, Windows Server 2012, Windows 7, Windows Vista SP2, Windows Server 2008 (Server Core Role not supported), Windows Server 2008 R2 (Server Core Role supported with SP1 or later; Itanium not supported)

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

Show:
© 2014 Microsoft