首页 > 开发 > 综合 > 正文

datagrid分页问题(前后跳页)《控件版》

2024-07-21 02:24:23
字体:
来源:转载
供稿:网友

最大的网站源码资源下载站,

把它定义在用户控件中的。在。ascx中写的。

在csdn上看了很多的datagrid分页问题,当然datagrid有自己的分页项,功能是很有限的,我也在csdn上看了很多自己分页的代码,发现都是用c#写的,我写了一个用asp。net中vb语言写的。以下代码供大家参考。

html代码:

<%@ control language="vb" autoeventwireup="false" codebehind="datagridpage.ascx.vb" inherits="datagridfenye.datagridpage" targetschema="http://schemas.microsoft.com/intellisense/ie5" %>
<link href="../css/style.css" type="text/css" rel="stylesheet">
<font face="ms ui gothic"></font>
<asp:panel id="divpanel" forecolor="blue" cssclass="font2" horizontalalign="left" width="84.16%"
runat="server" visible="true">
<asp:button id="btnnavfirst" runat="server" borderstyle="ridge" text="初頁" commandname="first"
borderwidth="1px" causesvalidation="false" backcolor="#e0e0e0" font-size="xx-small"></asp:button>
<asp:button id="btnnavprevious" runat="server" borderstyle="ridge" text="前頁" commandname="previous"
borderwidth="1px" causesvalidation="false" backcolor="#e0e0e0" font-size="xx-small"></asp:button>
<asp:button id="btnnavnext" runat="server" borderstyle="ridge" text="次頁" commandname="next"
borderwidth="1px" causesvalidation="false" backcolor="#e0e0e0" font-size="xx-small"></asp:button>
<asp:button id="btnnavlast" runat="server" borderstyle="ridge" text="末頁" commandname="last"
borderwidth="1px" causesvalidation="false" backcolor="#e0e0e0" font-size="xx-small"></asp:button><input id="hdrowcount" style="width: 43px; height: 22px" type="hidden" size="1" name="hidden1"
runat="server"> <input id="hdcurrentindex" style="width: 48px; height: 22px" type="hidden" size="2" value="0"
name="hidden1" runat="server">
<asp:label id="labelmsg" forecolor="blue" width="232px" runat="server"></asp:label>goto
<asp:textbox id="tbpage" width="31px" runat="server"></asp:textbox>
<asp:label id="labelmsg2" forecolor="blue" runat="server">頁</asp:label>
<asp:button id="btnnavgo" runat="server" borderstyle="ridge" text="確定" commandname="go" borderwidth="1px"
causesvalidation="false" backcolor="#e0e0e0" font-size="xx-small"></asp:button></asp:panel>


web代码:

imports system
imports system.data
imports system.drawing
imports system.text
imports system.web
imports system.web.ui.webcontrols
imports system.web.ui.htmlcontrols
public delegate sub binddatadelegate()
public class datagridpage

inherits system.web.ui.usercontrol

#region " web フォーム デザイナで生成されたコード "

'この呼び出しは web フォーム デザイナで必要です。
<system.diagnostics.debuggerstepthrough()> private sub initializecomponent()

end sub

public binddata1 as binddatadelegate
private _dg as datagrid
private _autohidden as boolean = true
private _pagesize as integer = 10
private _itemcount as integer = 0
private _dispstyle as integer = 1
protected withevents labelmsg as system.web.ui.webcontrols.label
protected withevents btnnavfirst as system.web.ui.webcontrols.button
protected withevents btnnavprevious as system.web.ui.webcontrols.button
protected withevents btnnavnext as system.web.ui.webcontrols.button
protected withevents btnnavlast as system.web.ui.webcontrols.button
protected withevents tbpage as system.web.ui.webcontrols.textbox
protected withevents labelmsg2 as system.web.ui.webcontrols.label
protected withevents btnnavgo as system.web.ui.webcontrols.button
protected withevents divpanel as system.web.ui.webcontrols.panel
protected withevents hdrowcount as system.web.ui.htmlcontrols.htmlinputhidden
protected withevents hdcurrentindex as system.web.ui.htmlcontrols.htmlinputhidden

'メモ : 次のプレースホルダ宣言は 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()
addhandler me.load, addressof page_load

end sub

#end region

public property target() as datagrid

get
return _dg
end get
set(byval value as datagrid)
_dg = value
end set
end property
public property autohidden() as boolean

get
return _autohidden
end get
set(byval value as boolean)
_autohidden = value
end set
end property

public property pagesize() as integer

get
return _pagesize
end get
set(byval value as integer)
_pagesize = value
end set
end property
public property itemcount() as integer

get
return _itemcount
end get
set(byval value as integer)
_itemcount = value
end set
end property

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

end sub
public sub settarget(byval adg as datagrid, byref abinddata1 as binddatadelegate)

_dg = adg

binddata1 = abinddata1

addhandler btnnavfirst.click, addressof navigationbuttonclick
addhandler btnnavprevious.click, addressof navigationbuttonclick
addhandler btnnavnext.click, addressof navigationbuttonclick
addhandler btnnavlast.click, addressof navigationbuttonclick
addhandler btnnavgo.click, addressof navigationbuttonclick
addhandler _dg.databinding, addressof zxdatabinding
binddata1()
end sub
public sub setstyle(byval apagesize as integer, byval aautohidden as boolean, byval adispstyle as integer)

_pagesize = apagesize
_autohidden = aautohidden
_dispstyle = adispstyle

divpanel.visible = not aautohidden
end sub
public sub setstyle(byval apagesize as integer, byval aautohidden as boolean)
setstyle(apagesize, aautohidden, 1)
end sub
public sub setstyle(byval apagesize as integer)
setstyle(apagesize, true)
end sub
public sub navigationbuttonclick(byval sender as object, byval e as system.eventargs)
dim direction as string
direction = sender.commandname

select case (direction.toupper())

case ("first")
_dg.currentpageindex = 0

case ("previous")
_dg.currentpageindex = math.max(_dg.currentpageindex - 1, 0)

case ("next")
_dg.currentpageindex = math.min(_dg.currentpageindex + 1, _dg.pagecount - 1)

case ("last")
_dg.currentpageindex = math.max(_dg.pagecount - 1, 0)

case ("go")
try
_dg.currentpageindex = math.min(_dg.pagecount - 1, cint(tbpage.text) - 1)
tbpage.text = ""
catch
tbpage.text = ""
end try

end select

binddata1()

end sub
public sub zxdatabinding(byval sender as object, byval e as system.eventargs)

dim newcount as integer = 0
dim pagecount as integer = 0

if (_dg.datasource is "null") then

setbuttonstate(0)
exit sub
end if
if (_dg.datasource.gettype().tostring().tolower() = "system.data.datatable") then

newcount = _dg.datasource.rows.count

elseif (_dg.datasource.gettype().tostring().tolower() = "system.data.dataview") then

newcount = _dg.datasource.count

elseif (_dg.datasource.gettype().tostring().tolower() = "system.data.dataset") then

newcount = _dg.datasource.tables(0).rows.count
end if

if (newcount > 0) then
pagecount = cint((newcount - 1) / _pagesize)
if (_dg.currentpageindex > pagecount - 1) then
_dg.currentpageindex = pagecount - 1
end if
else

pagecount = 0
_dg.currentpageindex = 0

end if

select case (_dispstyle)

case 1
labelmsg.text = "共" + pagecount.tostring() + "頁 第" + (_dg.currentpageindex + 1).tostring() + "頁"
labelmsg.text += " 總記?數:" + newcount.tostring() + ""

case 2
labelmsg.text = (_dg.currentpageindex + 1).tostring() + "/" + pagecount.tostring() + "頁"
labelmsg.text += " 總數:" + newcount.tostring()

end select

if (_autohidden) then

divpanel.visible = ((newcount - 1) / _pagesize > 0)

else

divpanel.visible = true

end if

setbuttonstate(pagecount)
end sub
public sub setbuttonstate(byval _pagecount as integer)

btnnavfirst.enabled = (_dg.currentpageindex > 0)
btnnavprevious.enabled = (_dg.currentpageindex > 0)
btnnavnext.enabled = (_dg.currentpageindex < _pagecount - 1)
btnnavlast.enabled = (_dg.currentpageindex < _pagecount - 1)
end sub


end class

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