这几天学习了一下自定义分页控件,现将实现方法记录下来,亲测可以实现:
1.首先创建一个.ascx文件,命名为TurnPage,然后在里面写控件前台展示的界面:
2.然后在TurnPage.ascx.cs里面写相应的后台代码,代码如下:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
|
namespace Web
{
public delegate void GoToPage( int PageNum);
public partial class TurnPage : System.Web.UI.UserControl
{
private GoToPage _GoToPage = null ;
protected void Page_Load( object sender, EventArgs e)
{
}
public void InitControl(GoToPage GP)
{
_GoToPage = GP;
}
public int DataCount
{
get { return Int32.Parse(lbl_TotalCount.Text); }
set { lbl_TotalCount.Text = value.ToString(); }
}
public int CurrPageNum
{
get { return Int32.Parse(lbl_CurrPage.Text); }
set { lbl_CurrPage.Text = value.ToString(); }
}
public int TotalPageNum
{
get { return Int32.Parse(lbl_TotalPage.Text); }
set { lbl_TotalPage.Text = value.ToString(); }
}
public int PageSize
{
get { return Int32.Parse(ddl_PageSize.SelectedValue); }
}
protected void btn_FristPage_Click( object sender, EventArgs e)
{
_GoToPage(1);
}
protected void btn_PrevPage_Click( object sender, EventArgs e)
{
if ( int .Parse(lbl_CurrPage.Text) > 1)
_GoToPage( int .Parse(lbl_CurrPage.Text) - 1);
else
_GoToPage(1);
}
protected void btn_NextPage_Click( object sender, EventArgs e)
{
if ( int .Parse(lbl_CurrPage.Text) < int .Parse(lbl_TotalPage.Text))
_GoToPage( int .Parse(lbl_CurrPage.Text) + 1);
else
_GoToPage( int .Parse(lbl_TotalPage.Text));
}
protected void btn_LastPage_Click( object sender, EventArgs e)
{
_GoToPage( int .Parse(lbl_TotalPage.Text));
}
public void ControlButtonClick()
{
if (DataCount > 0)
{
btn_FristPage.Enabled = true ;
btn_PrevPage.Enabled = true ;
btn_LastPage.Enabled = true ;
btn_NextPage.Enabled = true ;
}
else
{
btn_FristPage.Enabled = false ;
btn_PrevPage.Enabled = false ;
btn_LastPage.Enabled = false ;
btn_NextPage.Enabled = false ;
}
if (CurrPageNum == 1)
{
btn_FristPage.Enabled = false ;
btn_PrevPage.Enabled = false ;
}
if (CurrPageNum == TotalPageNum)
{
btn_LastPage.Enabled = false ;
btn_NextPage.Enabled = false ;
}
if (CurrPageNum == 0)
{
btn_FristPage.Enabled = false ;
btn_PrevPage.Enabled = false ;
btn_LastPage.Enabled = false ;
btn_NextPage.Enabled = false ;
}
}
protected void ddl_PageSize_SelectedIndexChanged( object sender, EventArgs e)
{
_GoToPage(1);
}
protected void btn_GO_Click( object sender, EventArgs e)
{
int pageNum;
if ( int .TryParse(txt_PageNum.Text, out pageNum))
{
if (pageNum > TotalPageNum)
_GoToPage(TotalPageNum);
else if (pageNum < 1)
_GoToPage(1);
else
_GoToPage(pageNum);
}
}
}}
|
3.控件的方法就写好了,现在要做的就是在其它页面引用这个控件了,引用方法如下:
然后在页尾还需配置一下:
4.这就是最后一步了,在你引用这个控件的页面的后台代码里写下以下语句:
大功告成了!
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持服务器之家。