SqlDataSourceStatusEventArgs 類別

定義

提供事件資料,在資料作業已完成之後,SqlDataSource 控制項會引發這個事件。

public ref class SqlDataSourceStatusEventArgs : EventArgs
public class SqlDataSourceStatusEventArgs : EventArgs
type SqlDataSourceStatusEventArgs = class
    inherit EventArgs
Public Class SqlDataSourceStatusEventArgs
Inherits EventArgs
繼承
SqlDataSourceStatusEventArgs

範例

下列程式碼範例示範如何使用 SqlDataSourceStatusEventArgs 類別來檢查使用 SqlDataSource 預存程式 GridView 填入控制項時所傳回之輸出參數的傳回值和值。 預存程式會選取 中顯示的 GridView 資料,但也會將其他資訊傳回給呼叫端,例如整數輸出參數和傳回值。 用於預存程式的參數 SqlDataSource 是由 SelectParameters 集合所包含,由參數組成,這些參數會將資訊從 Web 表單傳遞至預存程式,以及將資訊傳回至表單的參數。 Direction這些參數的 屬性會設定為 OutputReturnValue

<%@Page  Language="C#" %>
<%@Import Namespace="System.Data" %>
<%@Import Namespace="System.Data.Common" %>
<%@Import Namespace="System.Data.SqlClient" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<script runat="server">
// Clicking the Submit button explicitly refreshes the data 
// by calling the Select() method.
private void Submit(Object source, EventArgs e) {
  SqlDataSource1.Select(DataSourceSelectArguments.Empty);
}

// This event handler is called after the Select() method is executed.
private void OnSelectedHandler(Object source, SqlDataSourceStatusEventArgs e) {

  IDbCommand cmd = e.Command; 
  
  Label1.Text = "Parameter return values: ";

  foreach (SqlParameter param in cmd.Parameters) {
    //  Extract the value of the parameter.
    Label1.Text += param.ParameterName + " - " + param.Value.ToString();
  }
}
</script>

<html xmlns="http://www.w3.org/1999/xhtml" >
  <head runat="server">
    <title>ASP.NET Example</title>
</head>
<body>
    <form id="form1" runat="server">
        <asp:sqldatasource
            id="SqlDataSource1"
            runat="server"
            datasourcemode="DataSet"
            connectionstring="<%$ ConnectionStrings:MyNorthwind%>"
            selectcommand="getordertotal"
            onselected="OnSelectedHandler">
            <selectparameters>
              <asp:querystringparameter name="empId" querystringfield="empId" />
              <asp:parameter name="total" type="Int32" direction="Output" defaultvalue="0" />
              <asp:parameter name="_ret" type="Int32" direction="ReturnValue" defaultvalue="0" />
            </selectparameters>
        </asp:sqldatasource>
        <!--
          CREATE PROCEDURE dbo.getordertotal
            @empId int,
            @total int OUTPUT
          as
            set nocount on
            select @total    = count(1) from orders where employeeid=@empid;
            select * from orders where employeeID = @empId ;
            return (-1000);
          GO
        -->

        <asp:gridview
          id="GridView1"
          runat="server"
          allowpaging="True"
          pagesize="5"
          datasourceid="SqlDataSource1" />

        <asp:button
          id="Button1"
          runat="server"
          onclick="Submit"
          text="Refresh Data" />

        <asp:label id="Label1" runat="server" />

    </form>
  </body>
</html>
<%@Page  Language="VB" %>
<%@Import Namespace="System.Data" %>
<%@Import Namespace="System.Data.Common" %>
<%@Import Namespace="System.Data.SqlClient" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<script runat="server">
' Clicking the Submit button explicitly refreshes the data 
' by calling the Select() method.
Private Sub Submit(source As Object, e As EventArgs)
  
  SqlDataSource1.Select(DataSourceSelectArguments.Empty)
  
End Sub ' Submit

' This event handler is called after the Select() method is executed.
Private Sub OnSelectedHandler(source As Object, e As SqlDataSourceStatusEventArgs)

  Dim cmd As IDbCommand 
  cmd = e.Command
  Dim param As SqlParameter
  
  Label1.Text = "Parameter return values: "
  
  For Each param In cmd.Parameters
    
    ' Extract the name and value of the parameter.
    Label1.Text = Label1.Text & param.ParameterName & " - " & _
                  param.Value.ToString()

  Next

End Sub ' OnSelectedHandler
</script>

<html xmlns="http://www.w3.org/1999/xhtml" >
  <head runat="server">
    <title>ASP.NET Example</title>
</head>
<body>
    <form id="form1" runat="server">
        <asp:sqldatasource
            id="SqlDataSource1"
            runat="server"
            datasourcemode="DataSet"
            connectionstring="<%$ ConnectionStrings:MyNorthwind%>"
            selectcommand="getordertotal"
            onselected="OnSelectedHandler">
            <selectparameters>
              <asp:querystringparameter name="empId" querystringfield="empId" />
              <asp:parameter name="total" type="Int32" direction="Output" defaultvalue="0" />
              <asp:parameter name="_ret" type="Int32" direction="ReturnValue" defaultvalue="0" />
            </selectparameters>
        </asp:sqldatasource>
        <!--
          CREATE PROCEDURE dbo.getordertotal
            @empId int,
            @total int OUTPUT
          as
            set nocount on
            select @total    = count(1) from orders where employeeid=@empid;
            select * from orders where employeeID = @empId ;
            return (-1000);
          GO
        -->

        <asp:gridview
          id="GridView1"
          runat="server"
          allowpaging="True"
          pagesize="5"
          datasourceid="SqlDataSource1" />

        <asp:button
          id="Button1"
          runat="server"
          onclick="Submit"
          text="Refresh Data" />

        <asp:label id="Label1" runat="server" />

    </form>
  </body>
</html>

備註

類別 SqlDataSourceStatusEventArgs 用於 、 UpdatedInsertedDeleted 事件, Selected 以在資料來源控制項執行資料庫作業之後傳遞其相關資訊。 這項資訊包括作業所影響的資料列數目、 DbCommand 資料來源用來執行作業的物件,以及任何產生的例外狀況資訊。 藉由新增事件處理常式委派來處理 SelectedUpdatedInsertedDeleted 事件,您可以檢查此資料,並執行所需的任何其他後續處理。

控制項 SqlDataSource 會公開許多事件,您可以在資料作業期間處理基礎資料物件。 下表列出事件和相關聯的 EventArgs 和 事件處理常式類別,以更妥善地引導您使用 控制項對應至資料作業 SqlDataSource 生命週期的各種事件。

事件 EventArgs EventHandler
Selecting 會在擷取資料之前發生。 SqlDataSourceSelectingEventArgs SqlDataSourceSelectingEventHandler
Inserting、、 UpdatingDeleting 會在執行插入、更新或刪除作業之前發生。 SqlDataSourceCommandEventArgs SqlDataSourceCommandEventHandler
SelectedInsertedUpdated 會在 Deleted 資料擷取、插入、更新或刪除作業完成之後發生。 SqlDataSourceStatusEventArgs SqlDataSourceStatusEventHandler

建構函式

SqlDataSourceStatusEventArgs(DbCommand, Int32, Exception)

使用指定的輸出參數、傳回值,以及資料庫作業所影響的資料列數目,初始化 SqlDataSourceStatusEventArgs 類別的新執行個體。

屬性

AffectedRows

取得資料庫作業所影響的資料列數目。

Command

取得送出至資料庫的資料庫命令。

Exception

取得資料作業期間資料庫所擲回之任何例外狀況的包裝函式。

ExceptionHandled

取得或設定值,指出是否已處理資料庫擲回的例外狀況。

方法

Equals(Object)

判斷指定的物件是否等於目前的物件。

(繼承來源 Object)
GetHashCode()

做為預設雜湊函式。

(繼承來源 Object)
GetType()

取得目前執行個體的 Type

(繼承來源 Object)
MemberwiseClone()

建立目前 Object 的淺層複製。

(繼承來源 Object)
ToString()

傳回代表目前物件的字串。

(繼承來源 Object)

適用於

另請參閱