孜孜--程序人生

.net/j2me移动应用技术学习与研究

导航

[原创]wap中基于MobileControls.List的分页控件(vb扩展版)

Posted on 2006-05-19 16:28  孜孜  阅读(589)  评论(0)    收藏  举报
关键字:
wap分页
list分页

简介:看到LIST在手机上显示的分页效果,我个人感觉特别不满,随手写了一个用户控件与大家分享,有补充之处请您修整留言.

aspx代码
 1<%@ Control Language="vb" AutoEventWireup="false" Codebehind="BestList.ascx.vb" Inherits="MoaWap.BestList" TargetSchema="http://schemas.microsoft.com/Mobile/WebUserControl" %>
 2<%@ Register TagPrefix="mobile" Namespace="System.Web.UI.MobileControls" Assembly="System.Web.Mobile" %>
 3<mobile:list id="list" Decoration="Bulleted" runat="server"></mobile:list>
 4<mobile:Panel id="Panel1" runat="server" BreakAfter="True">
 5    <mobile:Label id="Label1" runat="server" BreakAfter="False">[</mobile:Label>
 6    <mobile:Label id="lbl_currpage" runat="server" BreakAfter="False">1</mobile:Label>
 7    <mobile:Label id="Label2" runat="server" BreakAfter="False">/</mobile:Label>
 8    <mobile:Label id="lbl_count" runat="server" BreakAfter="False">0</mobile:Label>
 9    <mobile:Label id="Label3" runat="server" BreakAfter="False">]  </mobile:Label>
10    <mobile:Label id="lbl_RoteCount" runat="server" BreakAfter="False">0</mobile:Label>
11    <mobile:Label id="Label5" runat="server" BreakAfter="False">条记录</mobile:Label>
12</mobile:Panel>
13<mobile:Panel id="Panel2" runat="server" BreakAfter="True">
14    <mobile:Command id="btn_first" runat="server" BreakAfter="False" Visible="False">[首页]</mobile:Command>
15    <mobile:command id="btn_syy" runat="server" BreakAfter="False" Visible="False">[上页]</mobile:command>
16    <mobile:command id="btn_xyy" runat="server" BreakAfter="False" Visible="False">[下页]</mobile:command>
17    <mobile:Command id="btn_end" runat="server" BreakAfter="False" Visible="False">[末页]</mobile:Command>
18</mobile:Panel>
19<mobile:Panel id="Panel3" runat="server">
20    <mobile:Label id="Label4" runat="server" BreakAfter="False">转向</mobile:Label>
21    <mobile:TextBox id="txt_go" runat="server" BreakAfter="False" Numeric="True" Size="2">1</mobile:TextBox>
22    <mobile:Command id="btn_go" runat="server" BreakAfter="False">GO</mobile:Command>
23</mobile:Panel>
24<mobile:Label id="lbl_index" runat="server" Visible="False">0</mobile:Label>
vb代码
  1Imports System.Web.UI.MobileControls
  2
  3委托定义
  7
  8Public MustInherit Class BestList
  9    Inherits System.Web.UI.MobileControls.MobileUserControl
 10
 11Web 窗体设计器生成的代码
 48
 49变量及事件声明
 55
 56    Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
 57        '在此处放置初始化页的用户代码
 58    End Sub

 59
 60属性定义
139
140    '''<summary>
141    '''列表框复位
142    '''</summary>
143    Public Sub ItemClear()
144        list.Items.Clear()
145        list.DataSource = Nothing
146        list.DataTextField = ""
147        list.DataValueField = ""
148        lbl_index.Text = "0"
149        lbl_currpage.Text = "1"
150    End Sub

151
152    '''ItemCommand事件激活
153    Private Sub list_ItemCommand(ByVal sender As ObjectByVal e As System.Web.UI.MobileControls.ListCommandEventArgs) Handles list.ItemCommand
154        lbl_index.Text = e.ListItem.Index.ToString()
155        'If Not (BestListItemCommand Is Nothing) Then
156        RaiseEvent BestListItemCommand(sender, e)
157        'End If
158    End Sub

159
160    '''<summary>
161    ''' 数据绑定
162    ''' </summary>
163    Public Sub DataBindList()
164        If Not (MyDataSoune Is NothingThen
165            '得出当前记录总数
166            Dim RoteCount As Integer = MyDataSoune.Rows.Count
167            '当不同数据源绑定时恢复当前页为1(根据记录数判断是否是同一个数据源)
168            Dim OldRoteCount As Integer = Convert.ToInt32(lbl_RoteCount.Text.Trim())
169            If OldRoteCount <> 0 And OldRoteCount <> RoteCount Then
170                lbl_currpage.Text = "1"
171            End If
172            lbl_RoteCount.Text = RoteCount.ToString()
173           '得出页总数
            Dim tespint As Integer = RoteCount Mod PageSize
            If tespint > 0 Then
                'lbl_count.Text = Convert.ToString(CInt(RoteCount / PageSize) + 1)
                lbl_count.Text = Convert.ToString(CInt((RoteCount - tespint) / PageSize) + 1)
176            Else
177                lbl_count.Text = Convert.ToString(RoteCount / PageSize)
178            End If
179            If Convert.ToInt32(lbl_currpage.Text.Trim()) > 0 And Convert.ToInt32(lbl_currpage.Text.Trim()) <= Convert.ToInt32(lbl_count.Text.Trim()) Then
180                '据当前页求出本页的开始记录和结束记录索引
181                Dim start As Integer, tempend As Integer
182                start = (Convert.ToInt32(lbl_currpage.Text.Trim()) - 1* PageSize
183                If Convert.ToInt32(lbl_currpage.Text.Trim()) = Convert.ToInt32(lbl_count.Text.Trim()) Then
184                    tempend = RoteCount - 1
185                Else
186                    tempend = start + PageSize - 1
187                End If
188                list.Items.Clear()
189                For i As Integer = start To tempend
190                    '初始化列表框
191                    Dim MyList As New MobileListItem
192                    MyList.Value = MyDataSoune.Rows(i)(list.DataValueField.Trim()).ToString()
193                    MyList.Text = MyDataSoune.Rows(i)(list.DataTextField.Trim()).ToString()
194                    list.Items.Add(MyList)
195                Next
196
197            End If
198            '---------导航按钮控制
199            If Convert.ToInt32(lbl_count.Text.Trim()) <= 1 Then
200                '当只有一页时或没有记录时
201                btn_first.Visible = False
202                btn_syy.Visible = False
203                btn_xyy.Visible = False
204                btn_end.Visible = False
205                Panel1.Visible = False
206            ElseIf Convert.ToInt32(lbl_currpage.Text.Trim()) = 1 Then
207                '当为第一页时
208                btn_first.Visible = False
209                btn_syy.Visible = False
210                btn_xyy.Visible = True
211                btn_end.Visible = True
212                Panel1.Visible = True
213            ElseIf Convert.ToInt32(lbl_currpage.Text.Trim()) = Convert.ToInt32(lbl_count.Text.Trim()) Then
214                '当为最后一页时
215                btn_first.Visible = True
216                btn_syy.Visible = True
217                btn_xyy.Visible = False
218                btn_end.Visible = False
219                Panel1.Visible = True
220            ElseIf Convert.ToInt32(lbl_currpage.Text.Trim()) = 2 Then
221                '当为第二页时
222                btn_first.Visible = True
223                btn_syy.Visible = False
224                btn_xyy.Visible = True
225                btn_end.Visible = True
226                Panel1.Visible = True
227            ElseIf Convert.ToInt32(lbl_currpage.Text.Trim()) = Convert.ToInt32(lbl_count.Text.Trim()) - 1 Then
228                '当为最后一页时
229                btn_first.Visible = True
230                btn_syy.Visible = True
231                btn_xyy.Visible = False
232                btn_end.Visible = True
233                Panel1.Visible = True
234            Else
235                btn_first.Visible = True
236                btn_syy.Visible = True
237                btn_xyy.Visible = True
238                btn_end.Visible = True
239                Panel1.Visible = True
240            End If
241        End If
242    End Sub

243
244    '''翻页处理
245    Private Sub MyPageIdexChange(ByVal key As String)
246        Select Case key
247            Case "first"
248                If Convert.ToInt32(lbl_count.Text.Trim()) > 0 Then
249                    lbl_currpage.Text = "1"
250                End If
251            Case "top"
252                If Convert.ToInt32(lbl_currpage.Text.Trim()) > 1 Then
253                    lbl_currpage.Text = Convert.ToString(Convert.ToInt32(lbl_currpage.Text.Trim()) - 1)
254                End If
255            Case "next"
256                If Convert.ToInt32(lbl_currpage.Text.Trim()) < Convert.ToInt32(lbl_count.Text.Trim()) Then
257                    lbl_currpage.Text = Convert.ToString(Convert.ToInt32(lbl_currpage.Text.Trim()) + 1)
258                End If
259            Case "last"
260                If Convert.ToInt32(lbl_count.Text.Trim()) > 0 Then
261                    lbl_currpage.Text = lbl_count.Text.Trim()
262                End If
263            Case "go"
264                If txt_go.Text.Trim() <> "" Then
265                    Dim igo As Integer = CInt(txt_go.Text.Trim())
266                    If igo <= Convert.ToInt32(lbl_count.Text.Trim()) And igo >= 1 Then
267                        lbl_currpage.Text = txt_go.Text.Trim()
268                    End If
269                End If
270        End Select
271        '触发用户控件的PageIdexChange事件
272        'If Not (BestListPageIndexChange Is Nothing) Then
273        RaiseEvent BestListPageIndexChange(Me)
274        'End If
275    End Sub

276
277
278    Private Sub btn_first_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn_first.Click
279        MyPageIdexChange("first")
280    End Sub

281
282    Private Sub btn_syy_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn_syy.Click
283        MyPageIdexChange("top")
284    End Sub

285
286    Private Sub btn_xyy_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn_xyy.Click
287        MyPageIdexChange("next")
288    End Sub

289
290    Private Sub btn_end_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn_end.Click
291        MyPageIdexChange("last")
292    End Sub

293
294    Private Sub btn_go_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn_go.Click
295        MyPageIdexChange("go")
296    End Sub

297End Class

298

引用页aspx代码
 1<%@ Page Language="vb" AutoEventWireup="false" Codebehind="dzgw_dbgw.aspx.vb" Inherits="MoaWap.dzgw_dbgw" %>
 2<%@ Register TagPrefix="uc1" TagName="BestList" Src="../BestList.ascx" %>
 3<%@ Register TagPrefix="mobile" Namespace="System.Web.UI.MobileControls" Assembly="System.Web.Mobile" %>
 4<HEAD>
 5    <meta content="Microsoft Visual Studio .NET 7.1" name="GENERATOR">
 6    <meta content="Visual Basic .NET 7.1" name="CODE_LANGUAGE">
 7    <meta content="http://schemas.microsoft.com/Mobile/Page" name="vs_targetSchema">
 8</HEAD>
 9<body Xmlns:mobile="http://schemas.microsoft.com/Mobile/WebForm">
10    <mobile:form id="FrmMain" title="待办公文" Paginate="True" runat="server">
11        <mobile:Panel id="Panel1" runat="server" BreakAfter="True">
12            <mobile:Link id="Link1" runat="server" BreakAfter="False" NavigateUrl="../main.aspx">主页</mobile:Link>
13            <mobile:Label id="Label2" runat="server" BreakAfter="False">&gt;</mobile:Label>
14            <mobile:Link id="Link2" runat="server" BreakAfter="False">电子公文</mobile:Link>
15            <mobile:Label id="Label1" runat="server" BreakAfter="False">&gt;待办公文</mobile:Label>
16        </mobile:Panel>
17        <uc1:BestList id="blist_dzgw" runat="server"></uc1:BestList>
18    </mobile:form>
19</body>
20

引用页 VB代码

 1Public Class dzgw_dbgw
 2    Inherits System.Web.UI.MobileControls.MobilePage
 3    Protected WithEvents Label1 As System.Web.UI.MobileControls.Label
 4    Protected WithEvents Link1 As System.Web.UI.MobileControls.Link
 5    Protected WithEvents Panel1 As System.Web.UI.MobileControls.Panel
 6    Protected WithEvents Link2 As System.Web.UI.MobileControls.Link
 7    Protected WithEvents Label2 As System.Web.UI.MobileControls.Label
 8    Protected WithEvents FrmMain As System.Web.UI.MobileControls.Form
 9    '声明自定义控件
10    Private WithEvents blist_dbgw As BestList
11
12Web 窗体设计器生成的代码
26
27    Private uid As String
28    ''用于事件关联
29    'Private WithEvents blist As BestList
30    Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
31        ''-------页面访问权限控制--------
32        'If Not Config.ChkPage(Convert.ToString(Session("xtqx")), "0102000000") Then
33        '    Me.RedirectToMobilePage("../index.aspx")
34        'End If
35        '获取用户控件
36        blist_dbgw = CType(Me.FindControl("blist_dzgw"), BestList)
37
38    End Sub

39
40    Private Sub blist_dbgw_ItemCommand(ByVal sender As ObjectByVal e As System.Web.UI.MobileControls.ListCommandEventArgs) Handles blist_dbgw.BestListItemCommand
41        If Not (e.ListItem Is NothingThen
42            'blist_dbgw.SelectIndex的使用
43            Response.Write(blist_dbgw.Items(blist_dbgw.SelectIndex).Text)
44        End If
45    End Sub

46
47    Private Sub blist_fgs_dbgweIndexChange(ByVal sender As ObjectHandles blist_dbgw.BestListPageIndexChange
48        '重新绑定一下数据
49        filldata()
50    End Sub

51
52    Private Sub FrmMain_Activate(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles FrmMain.Activate
53        filldata()
54    End Sub

55
56    Private Sub filldata()
57        Dim sqlstr As String = "select * from pt_login"
58        '初始化列表
59        blist_dbgw.DataTextField = "v_xm"
60        blist_dbgw.DataValueField = "v_dlid"
61        blist_dbgw.PageSize = 1
62        blist_dbgw.DataSoune = class_data.foundcnx(sqlstr, "t_dbsw").Tables(0)
63        blist_dbgw.DataBindList()
64    End Sub

65End Class

66
效果