孜孜--程序人生

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

导航

[原创]wap中基于MobileControls.List的分页控件(c#)

Posted on 2006-05-19 15:58  孜孜  阅读(1720)  评论(7编辑  收藏  举报
关键字:
wap分页
list分页

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

用户控件ASPX代码:
 1<%@ Register TagPrefix="mobile" Namespace="System.Web.UI.MobileControls" Assembly="System.Web.Mobile" %>
 2<%@ Control Language="c#" AutoEventWireup="false" Codebehind="BestList.ascx.cs" Inherits="MERPWAP.BestList" TargetSchema="http://schemas.microsoft.com/Mobile/WebUserControl" %>
 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:Label id="lbl_index" runat="server" Visible="False">0</mobile:Label>

<%@ Register TagPrefix="mobile" Namespace="System.Web.UI.MobileControls" Assembly="System.Web.Mobile" %>
cs代码

namespace MERPWAP
{
    
using System;
    
using System.Data;
    
using System.Drawing;
    
using System.Web;
    
using System.Web.Mobile;
    
using System.Web.UI.MobileControls;
    
using System.Web.UI.WebControls;
    
using System.Web.UI.HtmlControls;

    
public delegate void BestList_ItemCommand(object sender,System.Web.UI.MobileControls.ListCommandEventArgs e); 
    
public delegate void BestList_PageIndexChange(object sender); 
    
/// <summary>
    
///        BestList 的摘要说明。
    
/// </summary>

    public abstract class BestList : System.Web.UI.MobileControls.MobileUserControl
    
{
        
protected System.Web.UI.MobileControls.Command btn_xyy;
        
protected System.Web.UI.MobileControls.Command btn_syy;
        
protected System.Web.UI.MobileControls.List list_dxk;
//        private static int  CurrPage=1,PageCount = 0;
        protected System.Web.UI.MobileControls.List list;
        
protected System.Web.UI.MobileControls.Command btn_first;
        
protected System.Web.UI.MobileControls.Command btn_end;
        
protected System.Web.UI.MobileControls.Panel Panel1;
        
protected System.Web.UI.MobileControls.Label lbl_currpage;
        
protected System.Web.UI.MobileControls.Label lbl_count;
        
protected System.Web.UI.MobileControls.Label Label1;
        
protected System.Web.UI.MobileControls.Label Label2;
        
protected System.Web.UI.MobileControls.Label Label3;
        
protected System.Web.UI.MobileControls.Panel Panel2;
        
protected System.Web.UI.MobileControls.Label lbl_RoteCount;
        
protected System.Web.UI.MobileControls.Label Label5;
        
protected System.Web.UI.MobileControls.Label lbl_index;

# region 
"变量及事件声明"
        
private int MyPageSize =10;
        
private DataTable MyDataSoune;
        
public event BestList_ItemCommand BestListItemCommand; 
        
public event BestList_PageIndexChange BestListPageIndexChange; 
#endregion

        
private void Page_Load(object sender, System.EventArgs e)
        
{
            
//
            
        }


        
Web 窗体设计器生成的代码

        
/// <summary>
        
/// 列表集合
        
/// </summary>

        public System.Web.UI.MobileControls.MobileListItemCollection Items
        
{
            
get
            
{
                
return list.Items;
            }

        }

        
/// <summary>
        
/// 每页的记录数
        
/// </summary>

        public int PageSize
        
{
            
get
            
{
                
return MyPageSize;
            }

            
set 
            
{
                MyPageSize
=value;
            }

        }

        
/// <summary>
        
/// 当前索引号
        
/// </summary>

        public int SelectIndex
        
{
            
get
            
{
                
if(lbl_index.Text != "")
                
{
                    
return Convert.ToInt32(lbl_index.Text);
                }

                
else
                
{
                    
return -1;
                }

            }

            
set
            
{
                lbl_index.Text 
=Convert.ToString(value);
            }

        }

        
/// <summary>
        
/// 数据源表
        
/// </summary>

        public DataTable DataSoune
        
{
            
get
            
{
                
return MyDataSoune;
            }

            
set 
            
{
                MyDataSoune
=value;
            }

        }

        
/// <summary>
        
/// 列表文本数据字段
        
/// </summary>

        public string DataTextField
        
{
            
get
            
{
                
return list.DataTextField ;
            }

            
set 
            
{
                list.DataTextField
=value;
            }

        }

        
/// <summary>
        
/// 列表值数据字段
        
/// </summary>

        public string DataValueField
        
{
            
get
            
{
                
return list.DataValueField  ;
            }

            
set 
            
{
                list.DataValueField
=value;
            }

        }

        
/// <summary>
        
/// 页数
        
/// </summary>

        public int PageCount
        
{
            
get
            
{
                
return Convert.ToInt32(lbl_count.Text.Trim());
            }

        }

/// <summary>
/// 列表框复位
/// </summary>

        public void ItemClear()
        
{
            list.Items.Clear();
            list.DataSource 
=null;
            list.DataTextField
="";
            list.DataValueField 
="";
            lbl_index.Text 
="0";
            lbl_currpage.Text 
="1";
        }

        
private void list_ItemCommand(object sender, System.Web.UI.MobileControls.ListCommandEventArgs e)
        
{
            lbl_index.Text 
=e.ListItem.Index.ToString();
            
if (BestListItemCommand !=null) BestListItemCommand(sender,e);
        }
 
        
/// <summary>
        
/// 数据绑定
        
/// </summary>

        public void DataBindList()
        
{
            
if(MyDataSoune != null)
            
{
                
//得出当前记录总数
                int RoteCount=MyDataSoune.Rows.Count;
                
//当不同数据源绑定时恢复当前页为1(根据记录数判断是否是同一个数据源)
                int OldRoteCount = Convert.ToInt32(lbl_RoteCount.Text.Trim());
                
if(OldRoteCount != 0 && OldRoteCount != RoteCount)
                
{
                    lbl_currpage.Text 
= "1";
                }

                lbl_RoteCount.Text 
=RoteCount.ToString();
                
//得出页总数
                if((RoteCount % PageSize) > 0)
                
{
                    lbl_count.Text
=Convert.ToString((int)(RoteCount / PageSize) + 1);
                }

                
else
                
{
                    lbl_count.Text
=Convert.ToString(RoteCount / PageSize);
                }

                
if(Convert.ToInt32(lbl_currpage.Text.Trim()) >0 && Convert.ToInt32(lbl_currpage.Text.Trim()) <= Convert.ToInt32(lbl_count.Text.Trim()))
                
{
                    
//据当前页求出本页的开始记录和结束记录索引
                    int start,end;
                    start
=(Convert.ToInt32(lbl_currpage.Text.Trim()) - 1* PageSize ;
                    
if(Convert.ToInt32(lbl_currpage.Text.Trim()) == Convert.ToInt32(lbl_count.Text.Trim()))
                    
{
                        end 
= RoteCount - 1;
                    }

                    
else
                    
{
                        end 
=start + PageSize - 1;
                    }

                    list.Items.Clear();
                    
for(int i = start;i <= end;i++)
                    
{
                        
//初始化列表框
                        MobileListItem MyList=new MobileListItem();
                        MyList.Value 
=MyDataSoune.Rows[i][list.DataValueField.Trim()].ToString();
                        MyList.Text 
=MyDataSoune.Rows[i][list.DataTextField.Trim()].ToString();
                        list.Items.Add(MyList);
                    }

                }

                
//---------导航按钮控制
                if(Convert.ToInt32(lbl_count.Text.Trim()) <= 1)
                
{
                    
//当只有一页时或没有记录时
                    btn_first.Visible =false;
                    btn_syy.Visible 
=false;
                    btn_xyy.Visible 
=false;
                    btn_end.Visible 
=false;
                    Panel1.Visible 
=false;
                }

                
else if(Convert.ToInt32(lbl_currpage.Text.Trim()) == 1)
                
{
                    
//当为第一页时
                    btn_first.Visible =false;
                    btn_syy.Visible 
=false;
                    btn_xyy.Visible 
=true;
                    btn_end.Visible 
=true;
                    Panel1.Visible 
=true;
                }

                
else if(Convert.ToInt32(lbl_currpage.Text.Trim()) == Convert.ToInt32(lbl_count.Text.Trim()))
                
{
                    
//当为最后一页时
                    btn_first.Visible =true;
                    btn_syy.Visible 
=true;
                    btn_xyy.Visible 
=false;
                    btn_end.Visible 
=false;
                    Panel1.Visible 
=true;
                }

                
else if(Convert.ToInt32(lbl_currpage.Text.Trim()) == 2)
                
{
                    
//当为第二页时
                    btn_first.Visible =true;
                    btn_syy.Visible 
=false;
                    btn_xyy.Visible 
=true;
                    btn_end.Visible 
=true;
                    Panel1.Visible 
=true;
                }

                
else if(Convert.ToInt32(lbl_currpage.Text.Trim()) == Convert.ToInt32(lbl_count.Text.Trim()) - 1)
                
{
                    
//当为最后一页时
                    btn_first.Visible =true;
                    btn_syy.Visible 
=true;
                    btn_xyy.Visible 
=false;
                    btn_end.Visible 
=true;
                    Panel1.Visible 
=true;
                }

                
else
                
{
                    btn_first.Visible 
=true;
                    btn_syy.Visible 
=true;
                    btn_xyy.Visible 
=true;
                    btn_end.Visible 
=true;
                    Panel1.Visible 
=true;
                }

            }

        }

        
private void MyPageIdexChange(string key)
        
{
            
switch(key)
            
{
                
case "first" :
                    
if(Convert.ToInt32(lbl_count.Text.Trim()) > 0)
                        lbl_currpage.Text 
= "1";
                    
break;
                
case "top":
                    
if(Convert.ToInt32(lbl_currpage.Text.Trim()) > 1)
                        lbl_currpage.Text 
=Convert.ToString(Convert.ToInt32(lbl_currpage.Text.Trim()) - 1);
                    
break;
                
case "next":
                    
if(Convert.ToInt32(lbl_currpage.Text.Trim()) < Convert.ToInt32(lbl_count.Text.Trim()))
                        lbl_currpage.Text 
=Convert.ToString(Convert.ToInt32(lbl_currpage.Text.Trim()) + 1);
                    
break;
                
case "last":
                    
if(Convert.ToInt32(lbl_count.Text.Trim()) > 0)
                        lbl_currpage.Text 
=lbl_count.Text.Trim();
                    
break;
            }

            
//触发用户控件的PageIdexChange事件
            if (BestListPageIndexChange !=null) BestListPageIndexChange(this);
        }


        
private void btn_first_Click(object sender, System.EventArgs e)
        
{
            MyPageIdexChange(
"first");
        }


        
private void btn_syy_Click(object sender, System.EventArgs e)
        
{
            MyPageIdexChange(
"top");
        }


        
private void btn_xyy_Click(object sender, System.EventArgs e)
        
{
            MyPageIdexChange(
"next");
        }


        
private void btn_end_Click(object sender, System.EventArgs e)
        
{
            MyPageIdexChange(
"last");
        }

    }

}
 

调用页面

BestList blist_fgs;
        
private void Page_Load(object sender, System.EventArgs e)
        
{
//获取用户控件
            blist_fgs=(BestList)this.FindControl("blist_fgs");
//注册两个事件和对应的处理函数
            blist_fgs.BestListItemCommand += new BestList_ItemCommand(blist_fgs_ItemCommand); 
            blist_fgs.BestListPageIndexChange 
+= new BestList_PageIndexChange(blist_fgs_PageIndexChange);
}

private void blist_fgs_ItemCommand(object sender, System.Web.UI.MobileControls.ListCommandEventArgs e) 
        

            
if(e.ListItem != null)
            
{
                ActiveForm
=FrmSearchDXK;
            }

        }

//需在PageIndexChange重新填充娄据
        private void blist_fgs_PageIndexChange(object sender)
        
{
            filldata_fgs();
        }