SqlDataSourceStatusEventArgs 类     
定义
重要
一些信息与预发行产品相关,相应产品在发行之前可能会进行重大修改。 对于此处提供的信息,Microsoft 不作任何明示或暗示的担保。
为 SqlDataSource 控件在数据操作完成后引发的事件提供数据。
public ref class SqlDataSourceStatusEventArgs : EventArgspublic class SqlDataSourceStatusEventArgs : EventArgstype SqlDataSourceStatusEventArgs = class
    inherit EventArgsPublic Class SqlDataSourceStatusEventArgs
Inherits EventArgs- 继承
示例
下面的代码示例演示如何使用 SqlDataSourceStatusEventArgs 类检查使用带有存储过程的控件填充GridView控件时SqlDataSource返回的输出参数的返回值和值。 存储过程选择 中显示的 GridView数据,但也会将其他信息传递回调用方,例如整数输出参数和返回值。 用于存储过程的参数 SqlDataSource 包含在 集合中 SelectParameters ,由将信息从 Web 窗体传递到存储过程的参数以及将信息传递回窗体的参数组成。 Direction这些参数的 属性设置为 Output 和 ReturnValue。
<%@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 、Updated、 Inserted和 Deleted 事件中使用 Selected类,以在数据源控件执行数据库操作后传递有关该操作的信息。 此信息包括受操作影响的行数、 DbCommand 数据源用于执行操作的对象以及导致的任何异常信息。 通过添加事件处理程序委托来处理 Selected、 UpdatedInserted 或 Deleted 事件,可以检查此数据并执行所需的任何其他后处理。
控件 SqlDataSource 公开许多事件,可以在数据操作过程中处理这些事件来处理基础数据对象。 下表列出了事件以及关联的 EventArgs 和 事件处理程序类,以便更好地指导你使用 SqlDataSource 控件访问与数据操作的生命周期相对应的各种事件。
| 事件 | EventArgs | EventHandler | 
|---|---|---|
| Selecting 在检索数据之前发生。 | SqlDataSourceSelectingEventArgs | SqlDataSourceSelectingEventHandler | 
| Inserting、 Updating、 Deleting 发生在执行插入、更新或删除操作之前。 | SqlDataSourceCommandEventArgs | SqlDataSourceCommandEventHandler | 
| Selected、 Inserted、 Updated、、 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) |