共用方式為


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 Web 網頁會驗證使用者輸入未包含指令碼或 HTML 項目。 如需詳細資訊,請參閱 Script Exploits Overview (指令碼攻擊概觀)。

<%@ 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>

備註

按兩下頁面巡覽區欄位內的、 或控制件時Button,物件TemplatePagerField會引發 PagerCommandImageButtonLinkButton事件。 這些按鈕是可在欄位範本TemplatePagerFieldPagerTemplate定義的子控件。

在事件期間,您可以執行工作,例如變更要顯示的數據列數目,或變更頁面上第一個專案的索引。

如需如何處理事件的詳細資訊,請參閱 處理和引發事件

如需 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)

適用於

另請參閱