首页 > 编程 > .NET > 正文

ASP.net 2.0 自定义控件的开发之数据分页 第二章

2024-07-10 13:06:18
字体:
来源:转载
供稿:网友

以下为控件的主要部分 (ui 部分)
通知使用基于合成的实现的服务器控件创建它们包含的任何子控件,以便为回发或呈现做准备。
        protected overrides sub createchildcontrols()sub createchildcontrols()
            controls.clear()
            labpageinfo = new label()
            labpageinfotext_01 = new label
            labpageinfotext_01.text = "每页"
            txtpagesize = new textbox
            txtpagesize.id = "txtpagesize"
            txtpagesize.width = 66
            labpageinfotext_02 = new label
            labpageinfotext_02.text = "条记录"
            labpageinfotext_03 = new label
            labpageinfotext_03.text = "转到"
            labpageinfotext_04 = new label
            labpageinfotext_04.text = "页"
            txtpageindex = new textbox
            txtpageindex.id = "txtpageindex"
            txtpageindex.width = 66
            ibgotopage = new imagebutton
            ibgotopage.id = "ibgotopage"
            ibgotopage.width = 60
            ibgotopage.height = 20
            addhandler ibgotopage.click, addressof ibgotopage_click
            lbgotopage = new linkbutton
            lbgotopage.id = "lbgotopage"
            lbgotopage.text = "转到"
            addhandler lbgotopage.click, addressof lbgotopage_click
            lbfirstpage = new linkbutton
            lbfirstpage.id = "lbfirstpage"
            lbfirstpage.text = "首页"
            addhandler lbfirstpage.click, addressof lbfirstpage_click
            lbprevpage = new linkbutton
            lbprevpage.id = "lbprevpage"
            lbprevpage.text = "上页"
            addhandler lbprevpage.click, addressof lbprevpage_click
            lb下一页:asp.net 2.0 自定义控件的开发之数据分页 第三章 = new linkbutton
            lb下一页:asp.net 2.0 自定义控件的开发之数据分页 第三章.id = "lb下一页:asp.net 2.0 自定义控件的开发之数据分页 第三章"
            lb下一页:asp.net 2.0 自定义控件的开发之数据分页 第三章.text = "下页"
            addhandler lb下一页:asp.net 2.0 自定义控件的开发之数据分页 第三章.click, addressof lb下一页:asp.net 2.0 自定义控件的开发之数据分页 第三章_click
            lb上一页:asp.net 2.0 自定义控件的开发之数据分页 第一章 = new linkbutton
            lb上一页:asp.net 2.0 自定义控件的开发之数据分页 第一章.id = "lb上一页:asp.net 2.0 自定义控件的开发之数据分页 第一章"
            lb上一页:asp.net 2.0 自定义控件的开发之数据分页 第一章.text = "末页"
            addhandler lb上一页:asp.net 2.0 自定义控件的开发之数据分页 第一章.click, addressof lb上一页:asp.net 2.0 自定义控件的开发之数据分页 第一章_click
            me.controls.add(labpageinfo)
            me.controls.add(labpageinfotext_01)
            me.controls.add(txtpagesize)
            me.controls.add(labpageinfotext_02)
            me.controls.add(labpageinfotext_03)
            me.controls.add(txtpageindex)
            me.controls.add(ibgotopage)
            me.controls.add(lbgotopage)
            me.controls.add(lbfirstpage)
            me.controls.add(lbprevpage)
            me.controls.add(lb下一页:asp.net 2.0 自定义控件的开发之数据分页 第三章)
            me.controls.add(lb上一页:asp.net 2.0 自定义控件的开发之数据分页 第一章)
        end sub

重写控件的输出。
        protected overrides sub render()sub render(byval writer as htmltextwriter)
            addattributestorender(writer)
            writer.addattribute(htmltextwriterattribute.cellpadding, "1", false)
            writer.addattribute(htmltextwriterattribute.width, "100%", false)
            writer.renderbegintag(htmltextwritertag.table)
            writer.renderbegintag(htmltextwritertag.tr)
            writer.renderbegintag(htmltextwritertag.td)
            writer.addstyleattribute(htmltextwriterstyle.width, "50%")
            labpageinfo.text = "共 " & recordcount & "条记录 共 " & pagecount & " 页 当前第" & currentpageindex & "页"
            labpageinfo.rendercontrol(writer)
            writer.renderendtag()

            writer.renderbegintag(htmltextwritertag.td)
            writer.addstyleattribute(htmltextwriterstyle.width, "50%")
            labpageinfotext_01.rendercontrol(writer)
            writer.writeline(" ")
            txtpagesize.rendercontrol(writer)
            writer.writeline(" ")
            labpageinfotext_02.rendercontrol(writer)
            writer.writeline(" ")

            if gotostyle = stylegoto.imagebutton then
                labpageinfotext_03.rendercontrol(writer)
            else
                lbgotopage.rendercontrol(writer)
            end if
            writer.writeline(" ")
            txtpageindex.rendercontrol(writer)
            writer.writeline(" ")
            labpageinfotext_04.rendercontrol(writer)
            if gotostyle = stylegoto.imagebutton then
                ibgotopage.attributes.add("onmouseover", "this.src='" + hoverimageurl + "';")
                ibgotopage.attributes.add("onmouseout", "this.src='" + defaultimageurl + "';")
                ibgotopage.attributes.add("onmousedown", "this.src='" + pressedimageurl + "';")
                ibgotopage.rendercontrol(writer)
            end if
            writer.writeline(" ")
            lbfirstpage.rendercontrol(writer)
            writer.writeline(" ")
            lbprevpage.rendercontrol(writer)
            writer.writeline(" ")
            lb下一页:asp.net 2.0 自定义控件的开发之数据分页 第三章.rendercontrol(writer)
            writer.writeline(" ")
            lb上一页:asp.net 2.0 自定义控件的开发之数据分页 第一章.rendercontrol(writer)
            writer.renderendtag()
            writer.renderendtag()
            writer.renderendtag()
        end sub


由复合控件的标准设计器用于在设计时重建控件树。

msdn 中的解释重新创建派生自 compositecontrol 的控件的子控件。


       protected overrides sub recreatechildcontrols()sub recreatechildcontrols()
            ensurechildcontrols()
        end sub

数据库分页部分,后面将给出分页的sql server 存储过程。

protected function gotopage()function gotopage() as datatable
            dim dt as new datatable()
            conn = new sqlconnection(sqlconnection)
            conn.open()
            using conn
                dim command as new sqlcommand
                command.connection = conn
                command.commandtext = storedprocedurename
                command.commandtype = commandtype.storedprocedure
                dim parameter as sqlparameter
                parameter = command.parameters.add("@table", sqldbtype.nvarchar)
                parameter.value = tablename
                parameter = command.parameters.add("@primarykeyfield", sqldbtype.nvarchar)
                parameter.value = primarykeyfield
                parameter = command.parameters.add("@field", sqldbtype.nvarchar)
                parameter.value = field
                parameter = command.parameters.add("@where", sqldbtype.nvarchar)
                parameter.value = where
                parameter = command.parameters.add("@groupby", sqldbtype.nvarchar)
                parameter.value = groupby
                parameter = command.parameters.add("@orderby", sqldbtype.nvarchar)
                parameter.value = orderby
                parameter = command.parameters.add("@pagenumber", sqldbtype.nvarchar)
                parameter.value = currentpageindex
                parameter = command.parameters.add("@pagesize", sqldbtype.nvarchar)
                parameter.value = pagesize
                parameter = command.parameters.add("@recordcount", sqldbtype.int)
                parameter.direction = parameterdirection.output
                command.executenonquery()
                intrecordcount = command.parameters("@recordcount").value
                viewstate("recordcount") = intrecordcount
                viewstate("pagecount") = fix(intrecordcount / pagesize) + 1
                dim da as new sqldataadapter(command)
                da.fill(dt)
                return dt
            end using
        end function

数据绑定代码
public overrides sub databind()sub databind()
            '查找页面中的gridview及datalist并绑定数据
            dim objgridview as new gridview
            dim objdatalist as new datalist
            if controltopaginate = "" then
                exit sub
            end if
            _controltopaginat = page.findcontrol(controltopaginate)
            if (_controltopaginat is nothing) then
                exit sub
            end if
            if (typeof _controltopaginat is gridview) then
                objgridview = ctype(_controltopaginat, gridview)
                objgridview.datasource = gotopage()
                objgridview.databind()
            end if
            if (typeof _controltopaginat is datalist) then
                objdatalist = ctype(_controltopaginat, datalist)
                objdatalist.datasource = gotopage.defaultview
                objdatalist.databind()
            end if
        end sub

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