asp.net中ListView與DataPager分頁程式

來源:互聯網
上載者:User

頁面代碼如下:

 代碼如下 複製代碼
<asp:ListView runat="server" ID="_simpleTableListView">
  <LayoutTemplate>
    <table>
      <thead>
        <tr>
             <th id="Th1" runat="server">
                 ID</th>
             <th id="Th2" runat="server">
                 StudentID</th>
             <th id="Th3" runat="server">
                 Name</th>
             <th id="Th4" runat="server">
                 Math</th>
             <th id="Th5" runat="server">
                 English</th>
             <th id="Th6" runat="server">
                 Chinese</th>
        </tr>
      </thead>
      <tbody>
        <asp:PlaceHolder runat="server" ID="itemPlaceholder" />
      </tbody>
    </table>
  </LayoutTemplate>
  <ItemTemplate>
    <tr>
        <td>
            <asp:Label ID="IDLabel" runat="server" Text='<%# Eval("ID") %>' />
        </td>
        <td>
            <asp:Label ID="StudentIDLabel" runat="server" Text='<%# Eval("StudentID") %>' />
        </td>
        <td>
            <asp:Label ID="NameLabel" runat="server" Text='<%# Eval("Name") %>' />
        </td>
        <td>
            <asp:Label ID="MathLabel" runat="server" Text='<%# Eval("Math") %>' />
        </td>
        <td>
            <asp:Label ID="EnglishLabel" runat="server" Text='<%# Eval("English") %>' />
        </td>
        <td>
            <asp:Label ID="ChineseLabel" runat="server" Text='<%# Eval("Chinese") %>' />
        </td>
    </tr>
  </ItemTemplate>
</asp:ListView>
<asp:DataPager ID="DataPager2" runat="server"
    PagedControlID="_simpleTableListView" PageSize="2"
    onprerender="DataPager2_PreRender">
    <Fields>
        <asp:NextPreviousPagerField ButtonType="Button" ShowFirstPageButton="True"
            ShowLastPageButton="True" />
    </Fields>
</asp:DataPager>

後台代碼:
   

 代碼如下 複製代碼
protected void DataPager2_PreRender(object sender, EventArgs e)
    {
        BindData();
    }
 private void BindData()
    {
  using (OleDbConnection conn = new OleDbConnection(OleDbHelper.ConnectionString))
  {
   string sql = "select * from studentscore";
   OleDbDataAdapter dp=new OleDbDataAdapter(sql,conn);
   DataTable dt = new DataTable();
   dp.Fill(dt);
   _simpleTableListView.DataSource = dt;
   _simpleTableListView.DataBind();
  }
    }

注意:使用本方法,不需要在頁面的Page_Load事件中綁定資料到ListView控制項,
否則分頁會有問題。

第2種方法:

 代碼如下 複製代碼
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            BindData();
        }
    }
 protected void _simpleTableListView_PagePropertiesChanging(object sender, PagePropertiesChangingEventArgs e)
    {
        DataPager2.SetPageProperties(e.StartRowIndex, e.MaximumRows, false);
        BindData();
    }

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.