ASP.net 2.0 自定义控件的开发之数据分页 第二章
以下为控件的主要部分 (UI 部分)
通知使用基于合成的实现的服务器控件创建它们包含的任何子控件,以便为回发或呈现做准备。
Protected Overrides 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
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(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
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()
EnsureChildControls()
End Sub
EnsureChildControls()
End Sub
数据库分页部分,后面将给出分页的SQL Server 存储过程。
Protected 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
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()
'查找页面中的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
'查找页面中的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