实现了对DataList的分页 HTML控件的值需要转换web应用程序就可以取到了


1
/**//// <summary>
2
/// 当前页数
3
/// </summary>
4
int CurrentPage;
5
/**//// <summary>
6
/// 每页条数
7
/// </summary>
8
int PageSize;
9
/**//// <summary>
10
/// 总页数
11
/// </summary>
12
int PageCount;
13
/**//// <summary>
14
/// 总条数
15
/// </summary>
16
int RecordCount;
17
18
/**//// <summary>
19
/// 页面首次加载
20
/// </summary>
21
/// <param name="sender">发送对象</param>
22
/// <param name="e">事件参数</param>
23
protected void Page_Load(object sender, EventArgs e)
24
{
25
PageSize = 2;//每页数据记录
26
27
if (!IsPostBack)
28
{
29
CurrentPage = 0;//当前页习惯设为0
30
ViewState["PageIndex"] = 0;//页索引也设为0
31
32
//计算总共有多少记录
33
RecordCount = CalculateRecord();
34
35
//计算总共有多少页
36
if (RecordCount % PageSize == 0)
37
{
38
PageCount = RecordCount / PageSize;
39
}
40
else
41
{
42
PageCount = RecordCount / PageSize + 1;
43
}
44
45
this.TotalLbl.Text = PageCount.ToString();//显示总页数
46
ViewState["PageCount"] = PageCount;
47
48
this.DataListBind();
49
}
50
}
51
52
53
/**//// <summary>
54
/// 计算记录数
55
/// </summary>
56
/// <returns></returns>
57
private int CalculateRecord()
58
{
59
try
60
{
61
int recordCount;
62
SqlConnection con = new SqlConnection(ConfigurationManager.AppSettings["ConnectionString"].ToString());
63
con.Open();
64
65
string sql = "select count(*) as count from cps_adinfo";
66
SqlCommand cmd = new SqlCommand(sql, con);
67
SqlDataReader sdr = cmd.ExecuteReader();
68
69
if (sdr.Read())
70
{
71
recordCount = Int32.Parse(sdr["count"].ToString());
72
}
73
else
74
{
75
recordCount = 0;
76
}
77
78
sdr.Close();
79
con.Close();
80
81
return recordCount;
82
}
83
84
catch (Exception ex)
85
{
86
throw new Exception(ex.Message);
87
}
88
}
89
90
91
/**//// <summary>
92
/// 将数据绑定到Datalist控件
93
/// </summary>
94
public void DataListBind()
95
{
96
try
97
{
98
int StartIndex = CurrentPage * PageSize;//设定导入的起终地址
99
100
string sql = "select * from cps_adinfo";
101
102
DataSet ds = new DataSet();
103
SqlConnection con = new SqlConnection(System.Configuration.ConfigurationManager.AppSettings["ConnectionString"].ToString());
104
con.Open();
105
106
SqlDataAdapter sda = new SqlDataAdapter(sql, con);
107
//Fill方法的第一次重载
108
sda.Fill(ds, StartIndex, PageSize, "cps_adinfo");
109
110
this.TextDataList.DataSource = ds.Tables["cps_adinfo"].DefaultView;
111
this.TextDataList.DataBind();
112
this.PreviousLB.Enabled = true;
113
this.NextLB.Enabled = true;
114
if (CurrentPage == (PageCount - 1)) this.NextLB.Enabled = false;//当为最后一页时,下一页链接按钮不可用
115
if (CurrentPage == 0) this.PreviousLB.Enabled = false;//当为第一页时,上一页按钮不可用
116
this.CurrentLbl.Text = (CurrentPage + 1).ToString();//当前页数
117
118
}
119
120
catch (Exception ex)
121
{
122
throw new Exception(ex.Message);
123
}
124
}
125
126
/**//// <summary>
127
/// 按钮点击事件
128
/// </summary>
129
/// <param name="sender"></param>
130
/// <param name="e"></param>
131
public void LinkButton_Click(Object sender, CommandEventArgs e)
132
{
133
CurrentPage = Int32.Parse(ViewState["PageIndex"].ToString());//获得当前页索引
134
PageCount = Int32.Parse(ViewState["PageCount"].ToString());//获得总页数
135
136
string cmd = e.CommandName; //判断cmd,以判定翻页方向
137
switch (cmd)
138
{
139
case "prev"://上一页
140
if (CurrentPage > 0) CurrentPage--;
141
break;
142
143
case "next":
144
if (CurrentPage < (PageCount - 1)) CurrentPage++;//下一页
145
break;
146
147
case "first"://第一页
148
CurrentPage = 0;
149
break;
150
151
case "end"://最后一页
152
CurrentPage = PageCount - 1;
153
break;
154
155
case "jump"://跳转到第几页
156
if (this.TextBox1.Value.Trim() == ""
157
|| Int32.Parse(this.TextBox1.Value.Trim()) > PageCount
158
|| Int32.Parse(this.TextBox1.Value.Trim()) == 0)
159
160
{
161
return;
162
}
163
else
164
{
165
CurrentPage = Int32.Parse(this.TextBox1.Value.ToString()) - 1;
166
break;
167
}
168
}
169
ViewState["PageIndex"] = CurrentPage;//获得当前页
170
this.DataListBind();
171
172
}
分页跳转时做了判断 不会报异常了
下面是设计页

Code
1
<asp:DataList ID="TextDataList" runat="server" Width="100%" DataKeyField="adid" EnableViewState="False"
2
CssClass="pic" Font-Overline="False">
3
<ItemTemplate>
4
<p class="piczt">
5
文字链内容:
6
<asp:Label ID="lal" runat="server" Text='<%# Eval("adtitle") %>'></asp:Label><br />
7
</p>
8
<div class="picco">
9
<p class="picco1">
10
<asp:TextBox ID="txtHtmlCode" TextMode="MultiLine" Rows="3" Columns="40" Text='<%# Eval("htmlcode") %>'
11
runat="server" ReadOnly="true" EnableViewState="false"></asp:TextBox>
12
</p>
13
<p class="picco2">
14
<asp:TextBox ID="txtForumCode" TextMode="MultiLine" Rows="3" Columns="40" Text='<%# Eval("forumcode") %>'
15
runat="server" ReadOnly="true" EnableViewState="false"></asp:TextBox>
16
</p>
17
</div>
18
<div class="picco">
19
<p class="picco1">
20
<input name="button1" type="button" class="an2" value="复制广告代码" onclick="setTxt('<%# Eval("htmlcode") %>')"/>
21
复制广告代码</button>
22
</p>
23
<p class="picco2">
24
<input name="button1" type="button" class="an2" value="复制论坛代码" onclick="setTxt('<%# Eval("forumcode") %>')"/>
25
复制论坛代码</button>
26
</p>
27
</div>
28
<p class="piczt">
29
产品名称:鞋 产品ID:123456789</p>
30
<p class="piczt">
31
提成比例:10%</p>
32
<p class="piczt">
33
<span class="zi2">注意:</span> 您的联盟 ID, abcdef , 已经包含在代码中</p>
34
</ItemTemplate>
35
<ItemStyle Width="100%" />
36
</asp:DataList>
37
<div class="page">
38
共<asp:Label ID="TotalLbl" runat="server"></asp:Label>页, 当前第<asp:Label ID="CurrentLbl"
39
runat="server"></asp:Label>页
40
<asp:LinkButton ID="FirstLB" runat="server" OnCommand="LinkButton_Click" CommandName="first"
41
Font-Overline="False">[ 首页 | </asp:LinkButton>
42
<asp:LinkButton ID="PreviousLB" runat="server" OnCommand="LinkButton_Click" CommandName="prev"
43
Font-Overline="False">上一页 | </asp:LinkButton>
44
<asp:LinkButton ID="NextLB" runat="server" OnCommand="LinkButton_Click" CommandName="next"
45
Font-Overline="False">下一页 | </asp:LinkButton>
46
<asp:LinkButton ID="EndLB" runat="server" OnCommand="LinkButton_Click" CommandName="end"
47
Font-Bold="False" Font-Overline="False">末页 ]</asp:LinkButton>
48
<asp:LinkButton ID="JumpLB" runat="server" OnCommand="LinkButton_Click" CommandName="jump"
49
Font-Overline="False">跳到</asp:LinkButton>
50
<input id="TextBox1" runat="server" type="text" name="TextBox1" style="width: 30px"
51
onkeyup="value=value.replace(/[^\d]/g,'')" />
52
</div>
53
54
功能上除了分页 还有一个复制文本框的方法
<script type="text/javascript">
function setTxt(obj) {
window.clipboardData.setData('text', obj);
alert("复制成功!");
}
</script>


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

129

130

131

132



133

134

135

136

137

138



139

140

141

142

143

144

145

146

147

148

149

150

151

152

153

154

155

156

157

158

159

160



161

162

163

164



165

166

167

168

169

170

171

172

分页跳转时做了判断 不会报异常了
下面是设计页


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

<script type="text/javascript">
function setTxt(obj) {
window.clipboardData.setData('text', obj);
alert("复制成功!");
}
</script>