分享
 
 
 

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

王朝asp·作者佚名  2006-11-16
窄屏简体版  字體: |||超大  

以下为控件的主要部分 (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

lbNextPage = New LinkButton

lbNextPage.ID = "lbNextPage"

lbNextPage.Text = "下页"

AddHandler lbNextPage.Click, AddressOf lbNextPage_Click

lbLastPage = New LinkButton

lbLastPage.ID = "lbLastPage"

lbLastPage.Text = "末页"

AddHandler lbLastPage.Click, AddressOf lbLastPage_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(lbNextPage)

Me.Controls.Add(lbLastPage)

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(" ")

lbNextPage.RenderControl(writer)

writer.WriteLine(" ")

lbLastPage.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

 
 
 
免责声明:本文为网络用户发布,其观点仅代表作者个人观点,与本站无关,本站仅提供信息存储服务。文中陈述内容未经本站证实,其真实性、完整性、及时性本站不作任何保证或承诺,请读者仅作参考,并请自行核实相关内容。
2023年上半年GDP全球前十五强
 百态   2023-10-24
美众议院议长启动对拜登的弹劾调查
 百态   2023-09-13
上海、济南、武汉等多地出现不明坠落物
 探索   2023-09-06
印度或要将国名改为“巴拉特”
 百态   2023-09-06
男子为女友送行,买票不登机被捕
 百态   2023-08-20
手机地震预警功能怎么开?
 干货   2023-08-06
女子4年卖2套房花700多万做美容:不但没变美脸,面部还出现变形
 百态   2023-08-04
住户一楼被水淹 还冲来8头猪
 百态   2023-07-31
女子体内爬出大量瓜子状活虫
 百态   2023-07-25
地球连续35年收到神秘规律性信号,网友:不要回答!
 探索   2023-07-21
全球镓价格本周大涨27%
 探索   2023-07-09
钱都流向了那些不缺钱的人,苦都留给了能吃苦的人
 探索   2023-07-02
倩女手游刀客魅者强控制(强混乱强眩晕强睡眠)和对应控制抗性的关系
 百态   2020-08-20
美国5月9日最新疫情:美国确诊人数突破131万
 百态   2020-05-09
荷兰政府宣布将集体辞职
 干货   2020-04-30
倩女幽魂手游师徒任务情义春秋猜成语答案逍遥观:鹏程万里
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案神机营:射石饮羽
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案昆仑山:拔刀相助
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案天工阁:鬼斧神工
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案丝路古道:单枪匹马
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:与虎谋皮
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:李代桃僵
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:指鹿为马
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案金陵:小鸟依人
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案金陵:千金买邻
 干货   2019-11-12
 
推荐阅读
 
 
 
>>返回首頁<<
 
靜靜地坐在廢墟上,四周的荒凉一望無際,忽然覺得,淒涼也很美
© 2005- 王朝網路 版權所有