程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> .NET網頁編程 >> ASP.NET >> 關於ASP.NET >> ASP.NET GridView使用代碼總結

ASP.NET GridView使用代碼總結

編輯:關於ASP.NET

--注:由於是代碼的總結,所以文字比較少。

前台

<asp:Label ID="tplb" runat="server" Text="總頁數:"></asp:Label><asp:Label ID="lblPageCount"
                               runat="server" Text=""></asp:Label>&nbsp;&nbsp;
                             <asp:Label ID="curLabel" runat="server" Text="當前頁:"></asp:Label><asp:Label ID="lblPage" Text="1"
                               runat="server"></asp:Label>&nbsp;
                             <asp:LinkButton ID="lblFirstButton" runat="server" OnClick="lblFirstButton_Click" >|&lt;</asp:LinkButton>&nbsp;
                             <asp:LinkButton ID="lblPreButton" runat="server" OnClick="lblPreButton_Click" >&lt;</asp:LinkButton>&nbsp;
                             <asp:LinkButton ID="lblNextButton" runat="server" OnClick="lblNextButton_Click" >&gt;</asp:LinkButton>&nbsp;
                             <asp:LinkButton ID="lblLastButton" runat="server" OnClick="lblLastButton_Click" >&gt;|</asp:LinkButton>&nbsp;
                             <asp:DropDownList ID="ddlPage" runat="server" Width="40px" AutoPostBack="True"
                               OnSelectedIndexChanged="ddlPage_SelectedIndexChanged">
                               <asp:ListItem>10</asp:ListItem>
                               <asp:ListItem>15</asp:ListItem>
                               <asp:ListItem>20</asp:ListItem>
                               <asp:ListItem>30</asp:ListItem>
                             </asp:DropDownList>
                             <asp:Label ID="PageSizeLabel" runat="server" Text="條/頁">
</asp:Label>

後台

#region分頁

protected void BindFollowExamInfoGridView(int PersonID)
   {
     int currentpage = Convert.ToInt32(lblPage.Text);
     DataTable dt = new DataTable();
     dt = feibf.GetByPersonIDFollowExamInfo(PersonID); //查詢指定人的隨訪信息記錄
     if (dt.Rows.Count > 0)
     {
       FollowExamInfoGridView.DataSource = dt;
       FollowExamInfoGridView.DataBind();
       PagedDataSource ps = new PagedDataSource();
       ps.DataSource = dt.DefaultView;
       ps.AllowPaging = true;
       ps.PageSize = Convert.ToInt32(ddlPage.SelectedValue);
       lblPageCount.Text = ps.PageCount.ToString();
       this.lblPreButton.Enabled = true;
       this.lblNextButton.Enabled = true;
       ps.CurrentPageIndex = currentpage - 1;
       if (currentpage == 1)
       {
         this.lblPreButton.Enabled = false;
         this.lblFirstButton.Enabled = false;
       }
       else
       {
         this.lblPreButton.Enabled = true;
         this.lblFirstButton.Enabled = true;
       }
       if (currentpage == ps.PageCount)
       {
         this.lblNextButton.Enabled = false;
         this.lblLastButton.Enabled = false;
       }
       else
       {
         this.lblNextButton.Enabled = true;
         this.lblLastButton.Enabled = true;
       }
       FollowExamInfoGridView.DataSource = ps;
       FollowExamInfoGridView.DataBind();
     }

   }
   protected void lblPreButton_Click(object sender, EventArgs e)
   {
     this.lblPage.Text = Convert.ToString(Convert.ToUInt32(lblPage.Text) - 1);
     BindFollowExamInfoGridView(Convert.ToInt32(Request.QueryString["PersonID"]));
   }
   protected void lblNextButton_Click(object sender, EventArgs e)
   {
     this.lblPage.Text = Convert.ToString(Convert.ToUInt32(lblPage.Text) + 1);
     BindFollowExamInfoGridView(Convert.ToInt32(Request.QueryString["PersonID"]));
   }
   protected void lblFirstButton_Click(object sender, EventArgs e)
   {
     this.lblPage.Text = "1";
     BindFollowExamInfoGridView(Convert.ToInt32(Request.QueryString["PersonID"]));
   }
   protected void lblLastButton_Click(object sender, EventArgs e)
   {
     this.lblPage.Text = lblPageCount.Text;
     BindFollowExamInfoGridView(Convert.ToInt32(Request.QueryString["PersonID"]));
   }
   protected void ddlPage_SelectedIndexChanged(object sender, EventArgs e)
   {
     lblPage.Text = "1";
     BindFollowExamInfoGridView(Convert.ToInt32(Request.QueryString["PersonID"]));
   }
#endregion

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved