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)
イベントの種類
例
次の例は、イベントのイベントハンドラーを示して Selecting います。The following example shows an event handler for the Selecting event. ハンドラーは、Web ページ内の文字列値の配列から値を取得するクエリを作成します。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.
このイベントのハンドラーにカスタムの並べ替えまたはページングを実装する機能は、にバインドされているコントロールによって制限される場合があり LinqDataSource ます。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. たとえば、コントロールの列ヘッダーがクリックされると、 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 LinqDataSourceStatusEventArgs 。The exception will not be passed to an event handler for the Selected event (through the Exception property of the LinqDataSourceStatusEventArgs object). プロパティは、 Exception イベントの後にスローされる例外のみを格納し Selecting ます。The Exception property contains only the exceptions that are thrown after the Selecting event.