首页 > 开发 > 综合 > 正文

datagrid分页《非控件版》

2024-07-21 02:19:46
字体:
来源:转载
供稿:网友
首先对我之前的发表的那篇补充一下:当你在你的建立的工程中要用到我做的那个用户控件的话:声明 protected datagridpage1 as datagridpage,前是你拖进来的控件名,后是你定义用户控件。然后在你代码中要用你的控件就写上:

datagridpage1.settarget(mydatagrid, new binddatadelegate(addressof binddata))
datagridpage1.setstyle(10, false)

下划线部分是你自己写的一个绑定之类的涵数。sub,function 等。

以下是我不做成控件时的代码:

html:

<%@ page language="vb" autoeventwireup="false" codebehind="fenye.aspx.vb" inherits="datagridtitle.fenye"%>
<!doctype html public "-//w3c//dtd html 4.0 transitional//en">
<html>
<head>
<title>fenye</title>
<meta name="generator" content="microsoft visual studio .net 7.1">
<meta name="code_language" content="visual basic .net 7.1">
<meta name="vs_defaultclientscript" content="javascript">
<meta name="vs_targetschema" content="http://schemas.microsoft.com/intellisense/ie5">
</head>
<body ms_positioning="gridlayout">
<form id="form1" method="post" runat="server">
<font face="ms ui gothic">
<asp:datagrid id="mydatagrid" style="z-index: 101; left: 0px; position: absolute; top: 112px"
runat="server" width="744px" height="224px" allowpaging="true">
<pagerstyle visible="false"></pagerstyle>
</asp:datagrid>
<asp:linkbutton id="linkbutton1" style="z-index: 102; left: 8px; position: absolute; top: 96px"
runat="server" width="48px" height="8px">第一頁</asp:linkbutton>
<asp:linkbutton id="linkbutton2" style="z-index: 103; left: 64px; position: absolute; top: 96px"
runat="server" width="56px">前一頁</asp:linkbutton>
<asp:linkbutton id="linkbutton3" style="z-index: 104; left: 120px; position: absolute; top: 96px"
runat="server" width="48px">後一頁</asp:linkbutton>
<asp:linkbutton id="linkbutton4" style="z-index: 105; left: 176px; position: absolute; top: 96px"
runat="server" width="80px">最後一頁</asp:linkbutton>
<asp:label id="label1" style="z-index: 106; left: 552px; position: absolute; top: 80px" runat="server"
width="56px">跳轉到:</asp:label>
<asp:textbox id="txtgopage" style="z-index: 107; left: 624px; position: absolute; top: 80px"
runat="server" width="48px" height="24px"></asp:textbox>
<asp:button id="btngo" style="z-index: 108; left: 680px; position: absolute; top: 80px" runat="server"
height="24px" width="56px" text="button"></asp:button>
<asp:label id="lblcurrentindex" style="z-index: 109; left: 296px; position: absolute; top: 88px"
runat="server" height="24px" width="120px">label</asp:label>
<asp:label id="lblpagecount" style="z-index: 110; left: 424px; position: absolute; top: 88px"
runat="server" width="112px">label</asp:label></font>
</form>
</body>
</html>


web代码:

private sub page_load(byval sender as system.object, byval e as system.eventargs) handles mybase.load
' ページを初期化するユーザー コードをここに挿入します。
if not ispostback then

viewstate("strsort") = "orderid"

mydatagrid.datasource = getdv(viewstate("strsort").tostring())

mydatagrid.databind()
showstatspage()
'call orderbind()
end if
end sub
private function getdv(byval strsort as string) as dataview



dim dv as dataview

dim cn as new sqlconnection

try



cn.connectionstring = "data source=yangchangquan;initial catalog=northwind;persist security info=false;user id=sa;password=pass;"

cn.open()



dim adp as sqldataadapter = new sqldataadapter("select * from orders", cn)

dim ds as new dataset

adp.fill(ds)



dv = ds.tables(0).defaultview

catch ex as exception

#if debug then

session("error") = ex.tostring()

response.redirect("../error.aspx")

#end if

finally

'???接

cn.close()

end try

'排序

dv.sort = strsort

return dv

end function




private sub mydatagrid_sortcommand(byval source as system.object, byval e as system.web.ui.webcontrols.datagridsortcommandeventargs)
mydatagrid.currentpageindex = 0

'得到排序的列

viewstate("strsort") = e.sortexpression.tostring()

mydatagrid.datasource = getdv(viewstate("strsort").tostring())

mydatagrid.databind()

showstatspage()
end sub




private sub btngo_click(byval sender as system.object, byval e as system.eventargs) handles btngo.click
dim pagei as integer
if (txtgopage.text <> "") then
pagei = cint(trim(txtgopage.text)) - 1
if (pagei >= 0 and pagei < (mydatagrid.pagecount)) then
mydatagrid.currentpageindex = pagei
end if
end if
viewstate("strsort") = "orderid"

mydatagrid.datasource = getdv(viewstate("strsort").tostring())

mydatagrid.databind()
showstatspage()
end sub

private sub showstatspage()
lblcurrentindex.text = "[<font color='blue'>當前頁為:" & (mydatagrid.currentpageindex + 1) & "頁</font>]"
lblpagecount.text = "[<font color='blue'>共:" & mydatagrid.pagecount & "頁</font>]"

end sub

private sub linkbutton1_click(byval sender as system.object, byval e as system.eventargs) handles linkbutton1.click
mydatagrid.currentpageindex = 0
viewstate("strsort") = "orderid"

mydatagrid.datasource = getdv(viewstate("strsort").tostring())

mydatagrid.databind()
showstatspage()
end sub

private sub linkbutton4_click(byval sender as system.object, byval e as system.eventargs) handles linkbutton4.click
mydatagrid.currentpageindex = (mydatagrid.pagecount - 1)
viewstate("strsort") = "orderid"

mydatagrid.datasource = getdv(viewstate("strsort").tostring())

mydatagrid.databind()
showstatspage()
end sub

private sub linkbutton2_click(byval sender as system.object, byval e as system.eventargs) handles linkbutton2.click
if (mydatagrid.currentpageindex > 0) then
mydatagrid.currentpageindex = mydatagrid.currentpageindex - 1
viewstate("strsort") = "orderid"

mydatagrid.datasource = getdv(viewstate("strsort").tostring())

mydatagrid.databind()
showstatspage()
end if
end sub

private sub linkbutton3_click(byval sender as system.object, byval e as system.eventargs) handles linkbutton3.click
if (mydatagrid.currentpageindex < (mydatagrid.pagecount - 1)) then
mydatagrid.currentpageindex = mydatagrid.currentpageindex + 1
viewstate("strsort") = "orderid"

mydatagrid.datasource = getdv(viewstate("strsort").tostring())

mydatagrid.databind()
showstatspage()
end if
end sub

完成;

发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表