自已写的一个分页用户控件
由于做的一个网站中有好多地方用到分页,为了倫赖就写了这个了,记性不好,我怕过段时间忘了其中用到的一些东西!办法很笨,但以我现在的知识,只有用这种咯!
控件前台代码
------------------------------------------------------------------------------------
<table cellSpacing="0" cellPadding="0" width="100%" border="0">
<tr>
<td align="center"><b></b>
<hr width="100%" color="black">
</td>
</tr>
<tr>
<td style="HEIGHT: 19px" align="center">
<!--主要区域,用来显示数据记录 --><asp:label id="lab_table" runat="server"></asp:label></td>
</tr>
<tr align="center">
<td>
<hr width="100%" color="black">
<!-- 按钮-->
<table cellSpacing="0" cellPadding="0" border="0">
<tr align="center" height="25">
<td>当前面/总共页:</td>
<td align="left"><asp:label id="pagecount" Runat="server">1/10</asp:label> </td>
<td><asp:linkbutton id="LinkButton1" runat="server">前一页</asp:linkbutton> </td>
<td><asp:linkbutton id="LinkButton2" runat="server">下一页</asp:linkbutton> </td>
<td>跳转到: </td>
<td><asp:dropdownlist id="DropDownList1" runat="server" CssClass="tbox" Width="60px" Height="22px"></asp:dropdownlist> </td>
<td>
<asp:Button id="Button1" runat="server" Text="G O" BorderStyle="Groove" Height="22px"></asp:Button></td>
</tr>
</table>
<asp:Label id="lab_currentPage" runat="server" Visible="False"></asp:Label>
<asp:Label id="lab_allpage" runat="server" Visible="False"></asp:Label></td>
</tr>
</table>
<tr>
<td align="center"><b></b>
<hr width="100%" color="black">
</td>
</tr>
<tr>
<td style="HEIGHT: 19px" align="center">
<!--主要区域,用来显示数据记录 --><asp:label id="lab_table" runat="server"></asp:label></td>
</tr>
<tr align="center">
<td>
<hr width="100%" color="black">
<!-- 按钮-->
<table cellSpacing="0" cellPadding="0" border="0">
<tr align="center" height="25">
<td>当前面/总共页:</td>
<td align="left"><asp:label id="pagecount" Runat="server">1/10</asp:label> </td>
<td><asp:linkbutton id="LinkButton1" runat="server">前一页</asp:linkbutton> </td>
<td><asp:linkbutton id="LinkButton2" runat="server">下一页</asp:linkbutton> </td>
<td>跳转到: </td>
<td><asp:dropdownlist id="DropDownList1" runat="server" CssClass="tbox" Width="60px" Height="22px"></asp:dropdownlist> </td>
<td>
<asp:Button id="Button1" runat="server" Text="G O" BorderStyle="Groove" Height="22px"></asp:Button></td>
</tr>
</table>
<asp:Label id="lab_currentPage" runat="server" Visible="False"></asp:Label>
<asp:Label id="lab_allpage" runat="server" Visible="False"></asp:Label></td>
</tr>
</table>
控件后台代码
----------------------------------------------------------------------------------------------------------------------
public class Pagecontrol : System.Web.UI.UserControl
{
protected System.Web.UI.WebControls.DropDownList DropDownList1;
protected System.Web.UI.WebControls.Button Button1;
protected System.Web.UI.WebControls.LinkButton LinkButton1;
protected System.Web.UI.WebControls.LinkButton LinkButton2;
protected System.Web.UI.WebControls.Label lab_table;
protected System.Web.UI.WebControls.Label pagecount;
protected zqkjy.common.dbcom mydb;
//设置属性
private string strSql;
protected System.Web.UI.WebControls.Label lab_currentPage;
public string strsql //查询语句,语句中选择的字段数要和设置的列数相同
{
get {return strSql;}
set {strSql=value;}
}
private string strTitle; //行标题,传过来显的标题,注意用逗号隔开
public string strtitle
{
get {return strTitle;}
set {strTitle=value;}
}
private int intRows;
protected System.Web.UI.WebControls.Label lab_allpage;
//行数,一页多少行
public int introws
{
get {return intRows;}
set {intRows=value;}
}
private int intCols; //例数
public int intcols
{
get {return intCols;}
set {intCols=value;}
}
private void Page_Load(object sender, System.EventArgs e)
{
// 在此处放置用户代码以初始化页面
string strConn="Provider=Microsoft.Jet.OLEDB.4.0;Data Source="+Request.PhysicalApplicationPath+"/sys_data/"+System.Configuration.ConfigurationSettings.AppSettings["dbpath"].ToString();
mydb=new zqkjy.common.dbcom(strConn);
if(!Page.IsPostBack)
{
this.lab_currentPage.Text="1";
this.initial_dp(this.getPages());
this.lab_allpage.Text=this.getPages().ToString();
}
PagedDataSource mypd=this.retPagedDataSource(1);
if(mypd==null){this.lab_table.Text="没有任何据!";this.lab_currentPage.Text="0";}
this.showdate(mypd);
}
Web 窗体设计器生成的代码
显示数据
private void LinkButton1_Click(object sender, System.EventArgs e)
{
int iAllPage=Convert.ToInt32(this.lab_allpage.Text);
int iCrrentPage=Convert.ToInt32(this.lab_currentPage.Text);
if(iCrrentPage-1<=0)
{
return;
}
else
{
this.showdate(this.retPagedDataSource(iCrrentPage-1));
}
}
private void LinkButton2_Click(object sender, System.EventArgs e)
{
int iAllPage=Convert.ToInt32(this.lab_allpage.Text);
int iCrrentPage=Convert.ToInt32(this.lab_currentPage.Text);
if(iCrrentPage+1>iAllPage)
{
return;
}
else
{
this.showdate(this.retPagedDataSource(iCrrentPage+1));
}
}
private void Button1_Click(object sender, System.EventArgs e)
{
int iNum=Convert.ToInt32(this.DropDownList1.SelectedValue);
int iCrrentPage=Convert.ToInt32(this.lab_currentPage.Text);
if(iNum==iCrrentPage)
{
return;
}
else
{
this.showdate(this.retPagedDataSource(iNum));
}
}
}
{
protected System.Web.UI.WebControls.DropDownList DropDownList1;
protected System.Web.UI.WebControls.Button Button1;
protected System.Web.UI.WebControls.LinkButton LinkButton1;
protected System.Web.UI.WebControls.LinkButton LinkButton2;
protected System.Web.UI.WebControls.Label lab_table;
protected System.Web.UI.WebControls.Label pagecount;
protected zqkjy.common.dbcom mydb;
//设置属性
private string strSql;
protected System.Web.UI.WebControls.Label lab_currentPage;
public string strsql //查询语句,语句中选择的字段数要和设置的列数相同
{
get {return strSql;}
set {strSql=value;}
}
private string strTitle; //行标题,传过来显的标题,注意用逗号隔开
public string strtitle
{
get {return strTitle;}
set {strTitle=value;}
}
private int intRows;
protected System.Web.UI.WebControls.Label lab_allpage;
//行数,一页多少行
public int introws
{
get {return intRows;}
set {intRows=value;}
}
private int intCols; //例数
public int intcols
{
get {return intCols;}
set {intCols=value;}
}
private void Page_Load(object sender, System.EventArgs e)
{
// 在此处放置用户代码以初始化页面
string strConn="Provider=Microsoft.Jet.OLEDB.4.0;Data Source="+Request.PhysicalApplicationPath+"/sys_data/"+System.Configuration.ConfigurationSettings.AppSettings["dbpath"].ToString();
mydb=new zqkjy.common.dbcom(strConn);
if(!Page.IsPostBack)
{
this.lab_currentPage.Text="1";
this.initial_dp(this.getPages());
this.lab_allpage.Text=this.getPages().ToString();
}
PagedDataSource mypd=this.retPagedDataSource(1);
if(mypd==null){this.lab_table.Text="没有任何据!";this.lab_currentPage.Text="0";}
this.showdate(mypd);
}
Web 窗体设计器生成的代码
显示数据
private void LinkButton1_Click(object sender, System.EventArgs e)
{
int iAllPage=Convert.ToInt32(this.lab_allpage.Text);
int iCrrentPage=Convert.ToInt32(this.lab_currentPage.Text);
if(iCrrentPage-1<=0)
{
return;
}
else
{
this.showdate(this.retPagedDataSource(iCrrentPage-1));
}
}
private void LinkButton2_Click(object sender, System.EventArgs e)
{
int iAllPage=Convert.ToInt32(this.lab_allpage.Text);
int iCrrentPage=Convert.ToInt32(this.lab_currentPage.Text);
if(iCrrentPage+1>iAllPage)
{
return;
}
else
{
this.showdate(this.retPagedDataSource(iCrrentPage+1));
}
}
private void Button1_Click(object sender, System.EventArgs e)
{
int iNum=Convert.ToInt32(this.DropDownList1.SelectedValue);
int iCrrentPage=Convert.ToInt32(this.lab_currentPage.Text);
if(iNum==iCrrentPage)
{
return;
}
else
{
this.showdate(this.retPagedDataSource(iNum));
}
}
}
在其页中调用
----------------------------------------------------------------------------------------------------------------------------
uc1=(Pagecontrol)Page.LoadControl("Pagecontrol.ascx"); //加载控件
this.uc1.strtitle="列1,列2,列3,列4,列5";//设置列标题
this.uc1.strsql="select 列1,列2,列3,列4,列5 from tablename"//设置查询语句
this.uc1.introws=15;//设置行数
this.uc1.intcols=3;//设置列数
this.p1.Controls.Add(uc1);//p1为PlaceHolder控件
说明
------------------------------------------------------------------------------------------------------------------------