欢迎光临
我们一直在努力

asp.net 学习日记数据分页-.NET教程,Asp.Net开发

建站超值云服务器,限时71元/月

 

想来学习 .net 也2个月了吧,最初遇到的一个问题就是分页程序获取当前页的数据并支持字段排序而且支持搜索时的 where 语句 在网上搜了

很久也没找到,也在 asp.net 的一些论坛上发过帖子也许是那些高手懒得理我吧(做菜鸟就是难)也许是我找东西的能力有问题,但我却找到了以下sql 的

存储过程。

 set @page = (@page-1) * @pagesize + 1
 exec sp_cursoropen @p1 output, @strsql
 exec sp_cursorfetch @p1, 16, @page, @pagesize
 exec sp_cursorclose @p1

后来经过修改就成下面这样了(后面有个调用的例子 vb.net 版本我只会vb 不知微软何时出 masm.net 我想这辈子是没希望了)

 /* 通用存储过程分页—– 江建 
 只读取当前页 支持分类排序
*/

create procedure getpage
(
 @strtablename nvarchar(50),    –表名
 @fldname nvarchar (200),       –要返回的字段    
 @strwhere nvarchar(200)=””,    –where 语句 
 @fldorderby nvarchar(200),     –要排序的字段
 @ordertype int=0,          –排序类型升序还是降序  
 @page int = 1,    –要获取的页码
 @pagesize int = 5   –页大小
)
as
 set nocount on
 declare @p1 int
 declare @strsql  nvarchar(1000)
 declare @strorderby nvarchar(200)

 if @ordertype != 0
  set  @strorderby = order by [ + @fldorderby +] desc
 else

  set  @strorderby = order by [ + @fldorderby +] asc

 if @strwhere !=
  set @strsql=select + @fldname + from [ + @strtablename + ] where + @strwhere + @strorderby
 else
  set @strsql=select + @fldname + from [ + @strtablename + ]  + @strorderby
 

 set @page = (@page-1) * @pagesize + 1
 exec sp_cursoropen @p1 output, @strsql
 exec sp_cursorfetch @p1, 16, @page, @pagesize
 exec sp_cursorclose @p1
 go

/*这个是获取记录总数—– 江建*/
create procedure countrow
(
 @strtablename nvarchar(50),
 @fldnamecount nvarchar(50),
 @strwhere nvarchar(200)=””
)
as
 declare @strsql  nvarchar(1000)
 if @strwhere !=
  set @strsql=select count([ + @fldnamecount + ]) as countrow from [ + @strtablename + ] where +

@strwhere
 else
  set @strsql=select count([ + @fldnamecount + ]) as countrow from [ + @strtablename + ]
 
exec sp_executesql  @strsql
go

(1)下面是类模块 datapager.vb
=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
   data pager
   programme by jiang jian
   date:2005-06-23
   corpright(c) 2005 jiang jian.
=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
   http://vbcc.126.com
   email:jiangjian@ejet.com.cn
   this is class for sql database pager
=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
imports system
imports system.web.ui
imports system.web.ui.webcontrols
imports system.data
imports system.data.sqlclient

namespace datalayer

    public class getdata
        inherits webcontrol
        private myconn as new sqlconnection
        private introwcount as integer
        private strtablename as string
        private strfldnamecount as string
        private strfldname as string
        private intpagesize as integer
        private strwhere as string
        private strfldorderby as string

        public function connectiondatabase()
            dim strconn as string
            if not (myconn.state) then
                myconn.connectionstring = “server=(local);database=northwind;uid=sa”
                myconn.open()
            end if
        end function

        set or get tablename
        public property tablename() as string
            get
                return strtablename
            end get
            set(byval value as string)
                strtablename = value
            end set
        end property

        set or get count(xxxx)
        public property fldnamecount() as string
            get
                return strfldnamecount
            end get
            set(byval value as string)
                strfldnamecount = value
            end set
        end property

        set or get (xxxx) from
        public property fldname() as string
            get
                return strfldname
            end get
            set(byval value as string)
                strfldname = value
            end set
        end property

        set or get pagesize
        public property pagesize() as integer
            get
                return intpagesize
            end get
            set(byval value as integer)
                intpagesize = value
            end set
        end property
         set or get sql where
        public property where() as string
            get
                return strwhere
            end get
            set(byval value as string)
                strwhere = value
            end set
        end property

        set or get sql orderby
        public property fldorderby() as string
            get
                return strfldorderby
            end get
            set(byval value as string)
                strfldorderby = value
            end set
        end property

        public function getpagecount() as integer
            call connectiondatabase()

            dim rountcommand as sqldataadapter
            rountcommand = new sqldataadapter(“countrow”, myconn)
            rountcommand.selectcommand.commandtype = commandtype.storedprocedure

            rountcommand.selectcommand.parameters.add(new sqlparameter(“@strtablename”, sqldbtype.nvarchar, 50))
            rountcommand.selectcommand.parameters(“@strtablename”).value = strtablename

            rountcommand.selectcommand.parameters.add(new sqlparameter(“@fldnamecount”, sqldbtype.nvarchar, 50))
            rountcommand.selectcommand.parameters(“@fldnamecount”).value = strfldnamecount

            dim intcount as integer = rountcommand.selectcommand.executescalar()
            myconn.close()
            if (intcount mod intpagesize) > 0 then
                return (intcount \ intpagesize) + 1
            else
                return (intcount \ intpagesize)
            end if
        end function

        public function gotopage(optional byval intpage as integer = 1) as dataview
            call connectiondatabase()

            dim mycommand as sqldataadapter
            mycommand = new sqldataadapter(“getpage”, myconn)
            mycommand.selectcommand.commandtype = commandtype.storedprocedure

            mycommand.selectcommand.parameters.add(new sqlparameter(“@strtablename”, sqldbtype.nvarchar, 50))
            mycommand.selectcommand.parameters(“@strtablename”).value = strtablename

            mycommand.selectcommand.parameters.add(new sqlparameter(“@fldname”, sqldbtype.nvarchar, 200))
            mycommand.selectcommand.parameters(“@fldname”).value = strfldname

            mycommand.selectcommand.parameters.add(new sqlparameter(“@strwhere”, sqldbtype.nvarchar, 200))
            mycommand.selectcommand.parameters(“@strwhere”).value = strwhere

            mycommand.selectcommand.parameters.add(new sqlparameter(“@fldorderby”, sqldbtype.nvarchar, 200))
            mycommand.selectcommand.parameters(“@fldorderby”).value = strfldorderby

            mycommand.selectcommand.parameters.add(new sqlparameter(“@page”, sqldbtype.int))
            mycommand.selectcommand.parameters(“@page”).value = intpage

            mycommand.selectcommand.parameters.add(new sqlparameter(“@pagesize”, sqldbtype.int))
            mycommand.selectcommand.parameters(“@pagesize”).value = intpagesize

            dim ds as new dataset
            mycommand.fill(ds, “@employees”)

            return ds.tables(“@employees1”).defaultview
            myconn.close()
        end function
    end class
end namespace

(2)下面是webform1.aspx 的codebehind
imports bbs.datalayer
imports system
imports system.web
imports system.data
imports system.data.sqlclient

public class webform1
    inherits system.web.ui.page
    private clsgetdata as new getdata

#region ” web 窗体设计器生成的代码 “

    该调用是 web 窗体设计器所必需的。
    <system.diagnostics.debuggerstepthrough()> private sub initializecomponent()

    end sub
    protected withevents linkbutton1 as system.web.ui.webcontrols.linkbutton
    protected withevents datagrid1 as system.web.ui.webcontrols.datagrid
    protected withevents textbox1 as system.web.ui.webcontrols.textbox
    protected withevents button1 as system.web.ui.webcontrols.button
    protected withevents lnknext as system.web.ui.webcontrols.linkbutton
    protected withevents lnkprve as system.web.ui.webcontrols.linkbutton
    protected withevents label1 as system.web.ui.webcontrols.label
    protected withevents label2 as system.web.ui.webcontrols.label
    protected withevents label3 as system.web.ui.webcontrols.label

    注意: 以下占位符声明是 web 窗体设计器所必需的。
    不要删除或移动它。
    private designerplaceholderdeclaration as system.object

    private sub page_init(byval sender as system.object, byval e as system.eventargs) handles mybase.init
        codegen: 此方法调用是 web 窗体设计器所必需的
        不要使用代码编辑器修改它。
        initializecomponent()
    end sub

#end region

    private sub page_load(byval sender as system.object, byval e as system.eventargs) handles mybase.load
        if not ispostback then
            gridbind()
        end if
    end sub

    private sub gridbind()
        clsgetdata.tablename = “products”               表名
        clsgetdata.fldnamecount = “productid”           获取记录总数时所用到的字段
        clsgetdata.fldname = “productid,productname”    要返回的字段
        clsgetdata.pagesize = 10                        每页的大小
        if clsgetdata.fldorderby = “” then              排序的字段
            clsgetdata.fldorderby = “productid”
        end if

        if pagecount = 0 then
            pagecount = clsgetdata.getpagecount()
        end if

        if curpage = pagecount then
            lnknext.enabled = false
        else
            lnknext.enabled = true
        end if

        if curpage > 1 then
            lnkprve.enabled = true
        else
            lnkprve.enabled = false
        end if
        label3.text = “共 ” & pagecount & ” 页 当前第 ” & curpage & ” 页”
        datagrid1.datasource = clsgetdata.gotopage(curpage)
        datagrid1.databind()
    end sub

    private sub button1_click(byval sender as system.object, byval e as system.eventargs) handles button1.click
        if cint(textbox1.text) > pagecount then
            curpage = pagecount
            textbox1.text = curpage
        else
            curpage = convert.toint32(textbox1.text)
        end if
        gridbind()
    end sub

    private sub lnknext_click(byval sender as system.object, byval e as system.eventargs) handles lnknext.click
        if curpage < pagecount then
            curpage += 1
            gridbind()
        end if
    end sub

    private sub lnkprve_click(byval sender as object, byval e as system.eventargs) handles lnkprve.click
        if curpage > 1 then
            curpage -= 1
            gridbind()
        end if
    end sub

    public property pagecount() as integer
        get
            return viewstate(“pagecount”)
        end get

        set(byval value as integer)
            viewstate(“pagecount”) = value
        end set
    end property

    public property curpage() as integer
        get
            if viewstate(“curpage”) = 0 then
                viewstate(“curpage”) = 1
            end if
            return viewstate(“curpage”)
        end get

        set(byval value as integer)
            viewstate(“curpage”) = value
        end set
    end property

    private sub datagrid1_sortcommand(byval source as object, byval e as

system.web.ui.webcontrols.datagridsortcommandeventargs) handles datagrid1.sortcommand
        clsgetdata.fldorderby = e.sortexpression
        gridbind()
    end sub
end class

(3)下面是webform1.aspx 代码
<%@ page language=”vb” autoeventwireup=”false” codebehind=”webform1.aspx.vb” inherits=”bbs.webform1″%>
<html>
<head>
<title>webform1</title>
<meta name=vs_snaptogrid content=”false”>
<meta name=vs_showgrid content=”false”>
<meta content=”microsoft visual studio .net 7.1″ name=generator>
<meta content=”visual basic .net 7.1″ name=code_language>
<meta content=javascript name=vs_defaultclientscript>
<meta content=http://schemas.microsoft.com/intellisense/ie5 name=vs_targetschema>
</head>
<body ms_positioning=”gridlayout”>
<form id=”form1″ method=”post” runat=”server”>
<asp:datagrid id=datagrid1 style=”z-index: 101; left: 8px; position: absolute; top: 32px” runat=”server”

autogeneratecolumns=”false” width=”392px” height=”184px” allowsorting=”true”>
<columns>
<asp:boundcolumn datafield=”productid” sortexpression=”productid” headertext=”productid”></asp:boundcolumn>
<asp:boundcolumn datafield=”productname” sortexpression=”productname” headertext=”productname”></asp:boundcolumn>
</columns>
</asp:datagrid><asp:label id=”label2″ style=”z-index: 107; left: 318px; position: absolute; top: 12px” runat=”server”>页

</asp:label><asp:linkbutton id=”lnkprve” style=”z-index: 105; left: 8px; position: absolute; top: 8px”

runat=”server”>prvepage</asp:linkbutton><asp:linkbutton id=”lnknext” style=”z-index: 104; left: 92px; position: absolute;

top: 8px” runat=”server”>nextpage</asp:linkbutton>
<asp:textbox id=”textbox1″ style=”z-index: 102; left: 251px; position: absolute; top: 8px” runat=”server”

width=”57px”></asp:textbox>
<asp:button id=”button1″ style=”z-index: 103; left: 349px; position: absolute; top: 6px” runat=”server” width=”40px”

text=”go” height=”24px”>
</asp:button><asp:label id=”label1″ style=”z-index: 106; left: 179px; position: absolute; top: 11px” runat=”server”

height=”16px” width=”70px”>转到第</asp:label><asp:label id=”label3″ style=”z-index: 108; left: 8px; position: absolute; top:

300px” runat=”server” height=”18px” width=”317px”>xxxxx</asp:label>
</form>
</body>
</html>

赞(0)
版权申明:本站文章部分自网络,如有侵权,请联系:west999com@outlook.com 特别注意:本站所有转载文章言论不代表本站观点! 本站所提供的图片等素材,版权归原作者所有,如需使用,请与原作者联系。未经允许不得转载:IDC资讯中心 » asp.net 学习日记数据分页-.NET教程,Asp.Net开发
分享到: 更多 (0)