DataPagerCommandEventArgs 类

定义

PagerCommand 类的 TemplatePagerField 事件提供数据。

public ref class DataPagerCommandEventArgs : System::Web::UI::WebControls::CommandEventArgs
public class DataPagerCommandEventArgs : System.Web.UI.WebControls.CommandEventArgs
type DataPagerCommandEventArgs = class
    inherit CommandEventArgs
Public Class DataPagerCommandEventArgs
Inherits CommandEventArgs
继承
DataPagerCommandEventArgs

示例

以下示例演示如何使用 DataPagerCommandEventArgs 对象使用户能够指定将在 控件中显示的 ListView 数据页。 对象DataPagerCommandEventArgs将传递给 类的 TemplatePagerField 事件的处理程序PagerCommand

重要

此示例包含一个接受用户输入的文本框,这是一个潜在的安全威胁。 默认情况下,ASP.NET 网页验证用户输入是否不包含脚本或 HTML 元素。 有关详细信息,请参阅脚本侵入概述

<%@ Page language="C#" %>
    
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<script runat="server">

  // <Snippet2>
  protected void TemplatePagerField_OnPagerCommand(object sender, DataPagerCommandEventArgs e)
  {     
      // Get the new page number 
      TextBox PageNumberTextBox = (TextBox)e.Item.FindControl("PageNumberTextBox");
      int newPageNumber = -1;
      try
      {
        newPageNumber = Convert.ToInt32(PageNumberTextBox.Text.Trim());
      }
      catch (FormatException)
      {
        Message.Text = "Invalid page number.";
        return;
      }
      catch (OverflowException)
      {
        Message.Text = "Invalid page number.";
        return;
      }

      int newIndex = (newPageNumber - 1) * e.Item.Pager.PageSize;
      //Verify if the new index is valid
      if (newIndex >= 0 && newIndex <= e.TotalRowCount)
      {
        //Set the new start index and maximum rows
        e.NewStartRowIndex = newIndex;
        e.NewMaximumRows = e.Item.Pager.MaximumRows;
      }
      else
        Message.Text = "Invalid page number.";
  }
  // </Snippet2>

  protected void Page_Load(object sender, EventArgs e)
  {
    Message.Text = "";
  }
</script>

<html xmlns="http://www.w3.org/1999/xhtml" >
  <head id="Head1" runat="server">
    <title>DataPagerCommandEventArgs Example</title>    
    <style type="text/css">
      body 	
      {
        text-align: center;
        font: 12px Arial, Helvetica, sans-serif;
      }
      .item
      {
          border: 1px solid #8b7e66;
          background: white;
          min-height: 19px;
          width: 33%;
      }
      .alternatingItem
      {
        border: solid 1px #8b7e66;
        background: #f5deb3;
        width: 33%;
        min-height: 19px;
      }      
    </style>
  </head>
  <body>
    <form id="form1" runat="server">
        
    <h3>DataPagerCommandEventArgs Example</h3>

      <asp:ListView ID="ContactsListView" 
        DataSourceID="ContactsDataSource"
        GroupItemCount="3"
        runat="server">
        <LayoutTemplate>
          <table cellpadding="4" width="640px" id="tblProducts" runat="server">
            <tr runat="server" id="groupPlaceholder" />
          </table>
          <asp:DataPager runat="server" 
            ID="ContactsDataPager" 
            PageSize="30"
            PagedControlID="ContactsListView">
            <Fields>
              <asp:TemplatePagerField OnPagerCommand="TemplatePagerField_OnPagerCommand">
                <PagerTemplate>
                  <b>
                  Page
                  <asp:Label runat="server" ID="CurrentPageLabel" 
                    Text="<%# Container.TotalRowCount>0 ? (Container.StartRowIndex / Container.PageSize) + 1 : 0 %>" />
                  of
                  <asp:Label runat="server" ID="TotalPagesLabel" 
                    Text="<%# Math.Ceiling ((double)Container.TotalRowCount / Container.PageSize) %>" />
                  </b>              
                  <br /><br />
                  Jump to page:
                  <asp:TextBox ID="PageNumberTextBox" runat="server" 
                    Width="30px" 
                    Text="<%# Container.TotalRowCount>0 ? (Container.StartRowIndex / Container.PageSize) + 1 : 0 %>" />                      
                  <asp:Button ID="GoButton" runat="server" Text="Go" />
                  <br /><br />
                </PagerTemplate>
              </asp:TemplatePagerField>
            </Fields>
          </asp:DataPager>
        </LayoutTemplate>
        <GroupTemplate>
          <tr runat="server" id="ProductsRow">
            <td runat="server" id="itemPlaceholder" />
          </tr>
        </GroupTemplate>
        <ItemTemplate>
          <td class="item" runat="server">
            <asp:Label ID="NameLabel" runat="server" 
              Text='<%# Eval("LastName") + ", " + Eval("FirstName")%>' />
          </td>
        </ItemTemplate>
        <AlternatingItemTemplate>
          <td class="alternatingItem" runat="server">
            <asp:Label ID="NameLabel" runat="server" 
              Text='<%# Eval("LastName") + ", " + Eval("FirstName")%>' />
          </td>
        </AlternatingItemTemplate>
      </asp:ListView>
      <br />      

      <asp:Label ID="Message"
        ForeColor="Red"
        runat="server"/>

      <!-- This example uses Microsoft SQL Server and connects      -->
      <!-- to the AdventureWorks sample database. Use an ASP.NET    -->
      <!-- expression to retrieve the connection string value       -->
      <!-- from the Web.config file.                                -->
      <asp:SqlDataSource ID="ContactsDataSource" runat="server" 
            ConnectionString="<%$ ConnectionStrings:AdventureWorks_DataConnectionString %>"
            SelectCommand="SELECT [ContactID], [FirstName], [LastName] 
              FROM Person.Contact">
      </asp:SqlDataSource>
      
    </form>
  </body>
</html>
<%@ Page language="VB" %>
    
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<script runat="server">

  '<Snippet2>  
  Protected Sub TemplatePagerField_OnPagerCommand(ByVal sender As Object, _
    ByVal e As DataPagerCommandEventArgs)
    
    ' Get the new page number 
    Dim PageNumberTextBox As TextBox = _
      CType(e.Item.FindControl("PageNumberTextBox"), TextBox)
    
    Dim newPageNumber As Integer = -1
    Try
      newPageNumber = Convert.ToInt32(PageNumberTextBox.Text.Trim())
    Catch fex As FormatException
      Message.Text = "Invalid page number."
      Return
    Catch oex As OverflowException
      Message.Text = "Invalid page number."
      Return
    End Try
        
    Dim newIndex As Integer = _
      (newPageNumber - 1) * e.Item.Pager.PageSize
    
    'Verify if the new index is valid
    If newIndex >= 0 AndAlso newIndex <= e.TotalRowCount Then
      'Set the new start index and maximum rows
      e.NewStartRowIndex = newIndex
      e.NewMaximumRows = e.Item.Pager.MaximumRows
    Else
      Message.Text = "Invalid page number."
    End If
    
  End Sub
  ' </Snippet2>
     
  Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs)
    Message.Text = ""
  End Sub
    
</script>

<html xmlns="http://www.w3.org/1999/xhtml" >
  <head id="Head1" runat="server">
    <title>DataPagerCommandEventArgs Example</title>    
    <style type="text/css">
      body 	
      {
        text-align: center;
        font: 12px Arial, Helvetica, sans-serif;
      }
      .item
      {
          border: 1px solid #8b7e66;
          background: white;
          min-height: 19px;
          width: 33%;
      }
      .alternatingItem
      {
        border: solid 1px #8b7e66;
        background: #f5deb3;
        width: 33%;
        min-height: 19px;
      }      
    </style>
  </head>
  <body>
    <form id="form1" runat="server">
        
    <h3>DataPagerCommandEventArgs Example</h3>

      <asp:ListView ID="ContactsListView" 
        DataSourceID="ContactsDataSource"
        GroupItemCount="3"
        runat="server">
        <LayoutTemplate>
          <table cellpadding="4" width="640px" id="tblProducts" runat="server">
            <tr runat="server" id="groupPlaceholder" />
          </table>
          <asp:DataPager runat="server" 
            ID="ContactsDataPager" 
            PageSize="30"
            PagedControlID="ContactsListView">
            <Fields>
              <asp:TemplatePagerField OnPagerCommand="TemplatePagerField_OnPagerCommand">
                <PagerTemplate>
                  <b>
                  Page
                  <asp:Label runat="server" ID="CurrentPageLabel" 
                    Text="<%# IIf(Container.TotalRowCount>0, (Container.StartRowIndex / Container.PageSize) + 1 , 0) %>" />
                  of
                  <asp:Label runat="server" ID="TotalPagesLabel" 
                    Text="<%# Math.Ceiling (System.Convert.ToDouble(Container.TotalRowCount / Container.PageSize)) %>" />
                  </b>              
                  <br /><br />
                  Jump to page:
                  <asp:TextBox ID="PageNumberTextBox" runat="server" 
                    Width="30px" 
                    Text="<%# IIf(Container.TotalRowCount>0, (Container.StartRowIndex / Container.PageSize) + 1, 0) %>" />
                  <asp:Button ID="GoButton" runat="server" Text="Go" />
                  <br /><br />
                </PagerTemplate>
              </asp:TemplatePagerField>
            </Fields>
          </asp:DataPager>
        </LayoutTemplate>
        <GroupTemplate>
          <tr runat="server" id="ProductsRow">
            <td runat="server" id="itemPlaceholder" />
          </tr>
        </GroupTemplate>
        <ItemTemplate>
          <td id="Td1" class="item" runat="server">
            <asp:Label ID="NameLabel" runat="server" 
              Text='<%# Eval("LastName") & ", " & Eval("FirstName")%>' />
          </td>
        </ItemTemplate>
        <AlternatingItemTemplate>
          <td id="Td2" class="alternatingItem" runat="server">
            <asp:Label ID="NameLabel" runat="server" 
              Text='<%# Eval("LastName") & ", " & Eval("FirstName")%>' />
          </td>
        </AlternatingItemTemplate>
      </asp:ListView>
      <br />

      <asp:Label ID="Message"
        ForeColor="Red"
        runat="server"/>

      <!-- This example uses Microsoft SQL Server and connects      -->
      <!-- to the AdventureWorks sample database. Use an ASP.NET    -->
      <!-- expression to retrieve the connection string value       -->
      <!-- from the Web.config file.                                -->
      <asp:SqlDataSource ID="ContactsDataSource" runat="server" 
            ConnectionString="<%$ ConnectionStrings:AdventureWorks_DataConnectionString %>"
            SelectCommand="SELECT [ContactID], [FirstName], [LastName] 
              FROM Person.Contact">
      </asp:SqlDataSource>
      
    </form>
  </body>
</html>

注解

TemplatePagerField单击页管理器字段中的 、 LinkButtonImageButton 控件时Button,对象将引发 PagerCommand 事件。 这些按钮是在字段的TemplatePagerField模板中PagerTemplate定义的子控件。

在事件期间,可以执行更改要显示的行数或更改页面上第一项的索引等任务。

有关如何处理事件的详细信息,请参阅 处理和引发事件

有关 DataPagerCommandEventArgs 类的实例的初始属性值列表,请参见 DataPagerCommandEventArgs 构造函数。

构造函数

DataPagerCommandEventArgs(DataPagerField, Int32, CommandEventArgs, DataPagerFieldItem)

初始化 DataPagerCommandEventArgs 类的新实例。

属性

CommandArgument

获取命令的参数。

(继承自 CommandEventArgs)
CommandName

获取命令的名称。

(继承自 CommandEventArgs)
Item

获取 DataPagerFieldItem 对象,该对象包含 DataPagerField 对象以及 DataPagerField 对象的容器 DataPager 对象。

NewMaximumRows

获取或设置要在每个数据页上显示的最大记录数。

NewStartRowIndex

获取或设置要在数据页上显示的第一条记录的索引。

PagerField

获取包含单击的按钮的 DataPagerField 对象。

TotalRowCount

获取要显示的总记录数。

方法

Equals(Object)

确定指定对象是否等于当前对象。

(继承自 Object)
GetHashCode()

作为默认哈希函数。

(继承自 Object)
GetType()

获取当前实例的 Type

(继承自 Object)
MemberwiseClone()

创建当前 Object 的浅表副本。

(继承自 Object)
ToString()

返回表示当前对象的字符串。

(继承自 Object)

适用于

另请参阅