首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 网站开发 > asp.net >

小弟我想适当加些代码,用以下功能代码写一个web分页控件。希望们帮帮小弟我啊

2012-01-29 
我想适当加些代码,用以下功能代码写一个web分页控件。希望大虾们帮帮我啊!这是一个简单的分页代码:aspx:ta

我想适当加些代码,用以下功能代码写一个web分页控件。希望大虾们帮帮我啊!
这是一个简单的分页代码:

aspx:
<table   width= "100% ">
                        <tr>
                                <td   width= "100% "   style= "text-align:   right ">
                                        &nbsp; <span   style= "font-size:   10pt "> 总共: </span> <asp:Label   ID= "lblRecordCount "   runat= "server "
                                                ForeColor= "red "> </asp:Label> 条记录   当前: <asp:Label   ID= "lblCurrentPage "   runat= "server "
                                                        ForeColor= "Red "> </asp:Label> / <asp:Label   ID= "lblPageCount "   runat= "server "   ForeColor= "Red "> </asp:Label> 页
                                        转到:&nbsp; <asp:DropDownList   ID= "Ddl_PageNumber "   runat= "server "   AutoPostBack= "true "
                                                CssClass= "lanyu ">
                                        </asp:DropDownList> 页
                                        <asp:LinkButton   ID= "BtnFirst "   runat= "server "   CommandName= "First "   Font-Size= "10pt "
                                                OnCommand= "Page_OnClick "   Text= "首页 "> 首页 </asp:LinkButton>
                                        <asp:LinkButton   ID= "lbnPrevPage "   runat= "server "   CommandName= "prev "   Font-Size= "10pt "
                                                OnCommand= "Page_OnClick "   Text= "上一页 "> </asp:LinkButton> &nbsp;
                                        <asp:LinkButton   ID= "lbnNextPage "   runat= "server "   CommandName= "next "   Font-Size= "10pt "
                                                OnCommand= "Page_OnClick "   Text= "下一页 "> </asp:LinkButton>


                                        <asp:LinkButton   ID= "BtnLast "   runat= "server "   CommandName= "Last "   Font-Size= "10pt "   OnCommand= "Page_OnClick "
                                                Text= "尾页 "> 尾页 </asp:LinkButton>
                                </td>
                        </tr>
                </table>

cs:
public   partial   class   data_updGoods   :   System.Web.UI.Page
{
        string   constr   =   (ConfigurationManager.ConnectionStrings[ "jxcConnectionString "]).ToString();
        int   PageSize,   RecordCount,   PageCount,   CurrentPage,   i;
        ArrayList   Al_PageNum;

        public   void   Page_Load(Object   src,   EventArgs   e)
        {
                readnum();
        }
        public   void   readnum()
        {
                PageSize   =   6;                               //设定PageSize
                SqlConnection   MyConn   =   new   SqlConnection(constr);
                MyConn.Open();
                if   (!IsPostBack)                                     //第一次请求执行  
                {
                        RecordCount   =   CalculateRecord();   //计算总共有多少记录/

                        PageCount   =   RecordCount   /   PageSize;   //计算总共有多少页
                        if   (RecordCount   %   PageSize   >   0)           //取整  
                        {
                                PageCount   =   PageCount   +   1;
                        }
                        lblPageCount.Text   =   PageCount.ToString();
                        lblRecordCount.Text   =   RecordCount.ToString();
                        ViewState[ "PageCount "]   =   PageCount;


                        CurrentPage   =   0;
                        ViewState[ "PageIndex "]   =   0;

                        Al_PageNum   =   new   ArrayList();//绑定DROPDOWNLIST
                        for   (i   =   1;   i   <=   PageCount;   i++)       //从1开始循环,为了不出现0页码
                        {
                                Al_PageNum.Add(i.ToString());
                        }
                        Ddl_PageNumber.DataSource   =   Al_PageNum;
                        Ddl_PageNumber.DataBind();
                        ListBind();                                                   //绑定  
                }
                MyConn.Close();
        }
        public   int   CalculateRecord()                                 //计算总共有多少条记录
        {
                SqlConnection   MyConn   =   new   SqlConnection(constr);
                int   intCount;
                string   strCount   =   "select   count(*)   as   co   from   Goods ";
                SqlCommand   MyComm   =   new   SqlCommand(strCount,   MyConn);
                MyConn.Open();
                SqlDataReader   dr   =   MyComm.ExecuteReader();
                if   (dr.Read())
                {
                        intCount   =   Int32.Parse(dr[ "co "].ToString());
                }
                else
                {
                        intCount   =   0;
                }
                dr.Close();
                MyConn.Close();
                return   intCount;
        }

        public   void   ListBind()


        {
                SqlConnection   MyConn   =   new   SqlConnection(constr);
                int   StartIndex;                                                               //设定导入的起终地址  
                StartIndex   =   CurrentPage   *   PageSize;                         //计算记录数的起始点
                string   strSel   =   "select   *   from   Goods ";
                DataSet   ds   =   new   DataSet();
                SqlDataAdapter   MyAdapter   =   new   SqlDataAdapter(strSel,   MyConn);
                MyAdapter.Fill(ds,   StartIndex,   PageSize,   "Goods ");
                GridView1.DataSource   =   ds.Tables[ "Goods "].DefaultView;
                GridView1.DataBind();

                lbnNextPage.Enabled   =   true;
                lbnPrevPage.Enabled   =   true;
                BtnFirst.Enabled   =   true;
                BtnLast.Enabled   =   true;
                if   (PageCount   ==   0)
                {
                        lblCurrentPage.Text   =   "0 ";
                        lbnNextPage.Enabled   =   false;
                        lbnPrevPage.Enabled   =   false;
                        BtnFirst.Enabled   =   false;
                        BtnLast.Enabled   =   false;
                }
                else
                {
                        if   (CurrentPage   ==   (PageCount   -   1))
                        {
                                lbnNextPage.Enabled   =   false;
                                BtnLast.Enabled   =   false;
                        }
                        if   (CurrentPage   ==   0)


                        {
                                lbnPrevPage.Enabled   =   false;
                                BtnFirst.Enabled   =   false;
                        }
                        lblCurrentPage.Text   =   (CurrentPage   +   1).ToString();
                }
                Ddl_PageNumber.Text   =   lblCurrentPage.Text;
        }

        public   void   Page_OnClick(Object   sender,   CommandEventArgs   e)//上一页,下一页的单击代码
        {
                CurrentPage   =   (int)ViewState[ "PageIndex "];
                PageCount   =   (int)ViewState[ "PageCount "];
                string   cmd   =   e.CommandName;                                   //判断cmd,以判定翻页方向  
                switch   (cmd)
                {
                        case   "next ":
                                if   (CurrentPage   <   (PageCount   -   1))   CurrentPage++;
                                break;
                        case   "prev ":
                                if   (CurrentPage   >   0)   CurrentPage--;
                                break;
                        case   "Last ":
                                CurrentPage   =   (PageCount   -   1);
                                break;
                        default:
                                CurrentPage   =   0;
                                break;
                }

                ViewState[ "PageIndex "]   =   CurrentPage;
                ListBind();

        }


        public   void   PageNum_SelectIndexChanged(object   sender,   System.EventArgs   e)
        {
                ViewState[ "PageIndex "]   =   int.Parse(Ddl_PageNumber.SelectedItem.Value)   -   1;//保持不出现0页码
                //PageSize   =   3;
                CurrentPage   =   (int)ViewState[ "PageIndex "];
                PageCount   =   (int)ViewState[ "PageCount "];
                ListBind();
        }

        override   protected   void   OnInit(EventArgs   e)
        {
                this.Load   +=   new   System.EventHandler(this.Page_Load);
                this.Ddl_PageNumber.SelectedIndexChanged   +=   new   System.EventHandler(this.PageNum_SelectIndexChanged);
                base.OnInit(e);
        }
}

我没写过控件,希望借此学习如何自己编写自定义的控件。我会认真研究大侠们给的代码,希望大侠们多多帮助!

[解决办法]
JF

热点排行