Επεξεργασία

Κοινή χρήση μέσω


LinqDataSource.Selecting Event

Definition

Occurs before a data-retrieval operation.

public:
 event EventHandler<System::Web::UI::WebControls::LinqDataSourceSelectEventArgs ^> ^ Selecting;
public event EventHandler<System.Web.UI.WebControls.LinqDataSourceSelectEventArgs> Selecting;
member this.Selecting : EventHandler<System.Web.UI.WebControls.LinqDataSourceSelectEventArgs> 
Public Custom Event Selecting As EventHandler(Of LinqDataSourceSelectEventArgs) 

Event Type

Examples

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.
    }
}
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.

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

Remarks

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.

Applies to