ASP.NET 학습의 페이지 데이터 귀속GridView

30319 단어 GridView
프런트 페이지:
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="GridView      .aspx.cs" Inherits="      .GridView      " %>

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False">
           <Columns>
                <asp:TemplateField HeaderText="  ID">
                    <ItemTemplate><%#Eval("Uid") %></ItemTemplate>
                </asp:TemplateField>
                <asp:TemplateField HeaderText="  ">
                    <ItemTemplate><%#Eval("Subject") %></ItemTemplate>
                </asp:TemplateField>
                <asp:TemplateField HeaderText="  ">
                    <ItemTemplate><%#Eval("Content") %></ItemTemplate>
                </asp:TemplateField>
            </Columns>
        </asp:GridView>
           <asp:Label ID="TRecordCount" runat="server"></asp:Label>   
            <asp:Label ID="TPageSize" runat="server"></asp:Label>   
          <asp:Label ID="TPageCount" runat="server"></asp:Label>   
            <asp:Label ID="numberPage" runat="server"></asp:Label> 
        <asp:LinkButton ID="First" runat="server" CommandName="first" OnCommand="First_Command">  </asp:LinkButton>
        <asp:LinkButton ID="Pre" runat="server" CommandName="pre" OnCommand="First_Command">   </asp:LinkButton>
        <asp:LinkButton ID="Next" runat="server" CommandName="next" OnCommand="First_Command">   </asp:LinkButton>
        <asp:LinkButton ID="Last" runat="server" CommandName="last" OnCommand="First_Command">  </asp:LinkButton>
             <asp:DropDownList ID="ddl" runat="server" OnSelectedIndexChanged="ddl_SelectedIndexChanged">
            </asp:DropDownList> 
    </div>
    </form>
</body>
</html>

백그라운드 코드:
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Configuration;
using System.Data.SqlClient;
using System.Text;

namespace       
{
    /// <summary>
    /// GridView      
    ////// 1、    
    /// 1)    GridView  。          。     GridView1.AutoGenerateColumns = true;       
    /// 2)          【     、    、        、    、   、   、  、  、    】
    /// 
    /// 2、    
    /// 1)      :       、    、     、    
    /// 2)      :        ,  SQLDataAdapter      DataSet 
    /// 3)           ,    、    、        、           
    /// 4)        【            】 SQl  
    /// 5)     :         Sql       ,            
    /// 6)     :            ,       【       、         】
    /// 7)  、   、   、           :         
    /// 8)             
    /// 
    /// </summary>
    public partial class GridView       : System.Web.UI.Page
    {

        private int PageCount;     //   
        private int RecordCount;   //    
        private int PageSize=5;    //         //         
        private int CurrentPageIndex;  //      
        string strConnection = ConfigurationManager.ConnectionStrings["MyDB"].ToString();  //          
        /// <summary>
        ///     
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void Page_Load(object sender, EventArgs e)
        {
            #region      
            if (!IsPostBack)
            {  //     :            ,            
                //   PageCount、RecordCount、PageSize
                DataSet ds=GetDataSet();
                RecordCount = ds.Tables[0].Rows.Count;   //         
                //     
                if (RecordCount % PageSize == 0)
                {  //               ,   0,      。
                    PageCount = RecordCount / PageSize;
                }
                else
                {  //      
                    PageCount = RecordCount / PageSize + 1;
                }

                CurrentPageIndex = 1;  //   

                // lable      ,            。       
                TRecordCount.Text = RecordCount.ToString();
                TPageCount.Text = PageCount.ToString();
                TPageSize.Text = PageSize.ToString();
                numberPage.Text = CurrentPageIndex.ToString();

                // ddl  [    ]    
                for (int i = 1; i <= PageCount; i++)
                {
                    ddl.Items.Add(i.ToString());
                } 
            //        
            ShowData();
            }
            #endregion
            CurrentPageIndex = int.Parse(numberPage.Text.ToString());   //      ,  CurrentPageIndex
            PageCount = int.Parse(TPageCount.Text.ToString());   //      
        }

        /// <summary>
        ///            ,  SQLDataAdapter   DataSet 【           】
        /// </summary>
        public DataSet GetDataSet()
        {
            DataSet ds = new DataSet();
            using (SqlConnection sqlConn = new SqlConnection(strConnection))
            { //         
                string sqlStr = "select Uid,Subject,Content from tb_leaveWord";  //SQL    
                SqlDataAdapter sda = new SqlDataAdapter(sqlStr, sqlConn);
                //           ds   
                sda.Fill(ds);
            }
            return ds;
        }

        /// <summary>
        ///        
        /// </summary>
        /// <returns></returns>
        public DataSet GetEveryPageData()
        {
            DataSet ds = new DataSet();
            using (SqlConnection sqlconn = new SqlConnection(strConnection))
            {            
               string str = GetSqlStr();
               SqlDataAdapter sda = new SqlDataAdapter(str, sqlconn);
               sda.Fill(ds);
            }
            return ds;
        }

        /// <summary>
        ///   Sql  
        /// </summary>
        /// <returns></returns>
        public string GetSqlStr()
        {
            #region
            StringBuilder sb = new StringBuilder();
            sb.Append("select top(");
            sb.Append(PageSize.ToString());
            sb.Append(")Uid,Subject,Content from tb_LeaveWord where ID not in((select top(");
            sb.Append((PageSize * (CurrentPageIndex - 1)).ToString());
            sb.Append(")ID from tb_LeaveWord))");
            return sb.ToString(); 
            #endregion

            #region//StringBuilder sb = new StringBuilder();
            //sb.AppendFormat("select top({0})Uid,Subject,Content from tb_LeaveWord where ID not in((select top({1})ID from tb_LeaveWord))", PageSize.ToString(), (PageSize * (CurrentPageIndex - 1)).ToString());
            //return sb.ToString();
            #endregion

            #region//string str = "select top(" + PageSize.ToString() + ")Uid,Subject,Content from tb_LeaveWord where ID not in((select top(" + (PageSize * (CurrentPageIndex - 1)).ToString() + ")ID from tb_LeaveWord))";
            #endregion 


        }

        /// <summary>
        ///             
        /// </summary>
        public void ShowData()
        {
            DataSet Sds = GetEveryPageData();
            GridView1.DataSource = Sds;
            //                  ,        【               】
            //GridView1.AutoGenerateColumns = true;
            GridView1.DataBind();
            numberPage.Text = CurrentPageIndex.ToString();
            ddl.SelectedValue = CurrentPageIndex.ToString();   //         

            //        
            if(CurrentPageIndex==1){
                First.Enabled = false;
                Pre.Enabled = false;
            }
            else
            {
                First.Enabled = true;
                Pre.Enabled = true;
            }

            if (CurrentPageIndex==PageCount)
            {
                Next.Enabled = false;
                Last.Enabled = false;
            }
            else
            {
                Next.Enabled = true;
                Last.Enabled = true;
            }
        }

        /// <summary>
        ///          
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void ddl_SelectedIndexChanged(object sender, EventArgs e)
        {
            string num = ddl.SelectedValue;
            CurrentPageIndex = int.Parse(num);
            ShowData();
        }

        /// <summary>
        ///    、   、   、           
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void First_Command(object sender, CommandEventArgs e)
        {
            string result = e.CommandName;
            switch(result){
                case "first":
                    CurrentPageIndex = 1;
                    break;
                case "pre":
                    CurrentPageIndex--;
                    break;
                case "next":
                    CurrentPageIndex++;
                    break;
                case "last":
                    CurrentPageIndex = PageCount;
                    break;
            }
            ShowData();//    
        }

    }
}

좋은 웹페이지 즐겨찾기