LinqDataSource.Selecting 事件

定義

在資料擷取作業之前發生。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) 

事件類型

EventHandler<LinqDataSourceSelectEventArgs>

範例

下列範例顯示事件的事件處理常式 SelectingThe 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

下列範例示範如何將 Result 屬性指派給代表預存程式的方法所傳回的物件。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();  
}  

備註

處理事件,以便 Selecting 執行下列工作: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.

LinqDataSourceSelectEventArgs傳遞給這個事件之事件處理常式的物件包含資料抓取作業的參數。The LinqDataSourceSelectEventArgs object that is passed to event handlers for this event contains the parameters for the data-retrieval operation. 您可以在 Selecting 執行查詢之前,修改事件處理常式中的參數,也可以建立新的結果集,並將它指派給 Result 屬性。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.

您在這個事件的處理常式中執行自訂排序或分頁的能力,可能受限於系結至的控制項 LinqDataSourceYour ability to implement custom sorting or paging in handlers for this event might be limited by the control that is bound to the LinqDataSource. 例如,按一下控制項的資料行標頭時 GridView ,控制項會執行自動排序,這可能會覆寫您在事件處理常式中建立的任何順序。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.

如果事件的事件處理常式中擲回例外狀況 Selecting ,您必須在該事件處理常式中處理例外狀況。If an exception is thrown in an event handler for the Selecting event, you must handle the exception in that event handler. 例外狀況不會 Selected 透過物件) 的屬性,傳遞給事件 (的事件處理常式 Exception LinqDataSourceStatusEventArgsThe exception will not be passed to an event handler for the Selected event (through the Exception property of the LinqDataSourceStatusEventArgs object). Exception屬性只包含事件之後擲回的例外狀況 SelectingThe Exception property contains only the exceptions that are thrown after the Selecting event.

適用於