因为项目需要,要弄个有这样功能的控件。要有自定意分页和排序的功能。在这个控件里面数据的绑定都是用DataView来实现,DataView放在Session里,如果大家有更灵活的方法可以留言给我。Session的名字我已经做成了属性叫sessionName,可以直接设置,你们喜欢叫什么就自己改吧。以下是完整的代码,由于是刚刚出炉,可能会有考虑不足的地方,希望使用或修改的朋友多提意见一起交流,我分享我快乐:)
下面是可以使用的控件
先下载过的朋友,那个DLL文件有问题,下这个新的,下面的程序代码也改了一个小地方271行
该控件使用方法和DataGrid一样,需要分页时必须设置分页功能为true
LcDataGrid.rar
1using System;
2using System.Web.UI;
3using System.Web.UI.WebControls;
4using System.ComponentModel;
5using System.Data;
6
7namespace LcWebControls
8{
9 /// <summary>
10 /// 卢昶 2005-7-11
11 /// Email:darkangel@ynmail.com
12 /// msn:darkangellc@msn.com
13 /// </summary>
14 [DefaultProperty("Text"),
15 ToolboxData("<{0}:LcDataGrid runat=server></{0}:LcDataGrid>")]
16 public class LcDataGrid : System.Web.UI.WebControls.DataGrid
17 {
18#region Define
19 private string text;
20 private System.Web.UI.WebControls.Button bt=new Button();
21 private TextBox tb=new TextBox();
22 private System.Web.UI.Page pg=new Page();
23 private string sessionname;
24 private LinkButton PrevPAGE=new LinkButton();
25 private LinkButton NextPAGE=new LinkButton();
26 private DropDownList ddl=new DropDownList();
27 private Label lbend=new Label();
28 private Label lbtag=new Label();
29 private Label lbddls=new Label();
30 private Label lbddle=new Label();
31
32#endregion
33#region displayPortery
34
35 public int Currindex
36 {
37 set
38 {
39 try
40 {
41 this.CurrentPageIndex=value;
42 }
43 catch
44 {
45
46 }
47
48 }
49 get
50 {
51 return this.CurrentPageIndex;
52 }
53 }
54#endregion
55#region Property
56
57
58 [Bindable(true),
59 Category("Appearance"),
60 DefaultValue("")]
61 public string Text
62 {
63 get
64 {
65 return text;
66 }
67
68 set
69 {
70 text = value;
71 }
72 }
73 [Bindable(true),
74 Category("Appearance"),
75 DefaultValue(""),
76 DescriptionAttribute("Session的名称,Session用于数据绑定,必须是DataView类型")
77 ]
78 public string sessionName
79 {
80 get
81 {
82 return sessionname;
83 }
84 set
85 {
86 sessionname=value;
87
88 }
89 }
90 [Bindable(true),
91 Category("Appearance"),
92 DefaultValue("")]
93 public string TT
94 {
95 get
96 {
97 try
98 {
99
100 return this.ViewState["sort"].ToString();
101 }
102 catch
103 {
104 this.ViewState["sort"]="asc";
105 return this.ViewState["sort"].ToString();
106 }
107
108 }
109 set
110 {
111 this.ViewState["sort"]=value;
112 }
113
114 }
115
116 #endregion
117
118 /// <summary>
119 /// 将此控件呈现给指定的输出参数。
120 /// </summary>
121 /// <param name="output"> 要写出到的 HTML 编写器 </param>
122
123protected override void OnItemCreated(DataGridItemEventArgs e)
124 {
125
126 if(e.Item.ItemType == ListItemType.Pager)
127 {
128 e.Item.Cells[0].Controls.Clear();
129 TextBox tb=new TextBox();
130 creatControl(e.Item.Cells[0]);
131 }
132
133
134 }
135protected override void OnItemCommand(DataGridCommandEventArgs e)
136 {
137 string strCommandName = e.CommandName;
138 if(e.CommandName == null)
139 return;
140
141 strCommandName = string.IsInterned(strCommandName);
142 switch(e.CommandName)
143 {
144
145 case "PrevPAGE":
146 {
147 PrevPAGE_Click(null,null);
148 break;
149 }
150 case "NextPAGE":
151 {
152 NextPAGE_Click(null,null);
153 break;
154 }
155
156 }
157 base.OnItemCommand(e);
158 }
159
160protected override void OnItemDataBound(DataGridItemEventArgs e)
161{
162
163 if(e.Item.ItemType==ListItemType.Item||e.Item.ItemType==ListItemType.AlternatingItem)
164 {
165 e.Item.Attributes.Add("style","cursor:hand");
166 e.Item.Attributes.Add("onmouseover","this.style.backgroundColor='#FFCC00'");
167 e.Item.Attributes.Add("onmouseout","this.style.backgroundColor=''");
168 }
169}
170 protected override void OnSortCommand(DataGridSortCommandEventArgs e)
171 {
172 base.OnSortCommand (e);
173 DataView dv=(DataView)this.Page.Session[sessionName];
174 if (TT.Equals("desc"))
175 {
176 dv.Sort=e.SortExpression+" asc";
177 TT="asc";
178 }
179 else
180 {
181 dv.Sort=e.SortExpression+" desc";
182 TT="desc";
183 }
184
185
186 this.DataSource=dv;
187 this.DataBind();
188 this.Page.Session[sessionName]=dv;
189 }
190
191
192
193
194#region InitDataGird
195
196 protected void creatControl(TableCell tc)
197 {
198
199 creatPageCount(tc);
200 creatCount(tc);
201 creatSPage(tc);
202 creatDDL(tc);
203
204
205 }
206 private void creatCount(TableCell tc)
207 {
208
209 lbtag.Text=" 每页显示记录数";
210 tb.Text=this.PageSize.ToString();
211 tb.Width=20;
212 bt.Text="提交";
213 bt.Click+=new EventHandler(bt_Click);
214 tc.Controls.Add(lbtag);
215 tc.Controls.Add(tb);
216 tc.Controls.Add(bt);
217 //
218
219 }
220
221 private void creatPageCount(TableCell tc)
222 {
223 Label lb=new Label();
224 lb.Text="<P align=\"right\"> 共"+this.PageCount+"页 第"+(this.CurrentPageIndex+1)+"页 ";
225 tc.Controls.Add(lb);
226
227 }
228
229 private void creatSPage(TableCell tc)
230 {
231 PrevPAGE.Text="上一页";
232 NextPAGE.Text="下一页";
233 PrevPAGE.CommandName="PrevPAGE";
234 NextPAGE.CommandName="NextPAGE";
235 tc.Controls.Add(PrevPAGE);
236 tc.Controls.Add(NextPAGE);
237 }
238 private void creatDDL(TableCell tc)
239 {
240
241 lbend.Text="页</p>";
242 lbddls.Text=" 跳转到第";
243 ddl.AutoPostBack=true;
244 ddl.EnableViewState=true;
245 if (!this.Page.IsPostBack)
246 {
247 ddl.Items.Clear();
248 initDDL();
249 }
250 ddl.SelectedIndexChanged+=new EventHandler(ddl_SelectedIndexChanged);
251 tc.Controls.Add(lbddls);
252 tc.Controls.Add(ddl);
253 tc.Controls.Add(lbend);
254 }
255 private void initDDL()
256 {
257 int count=this.PageCount;
258
259 for (int i=1;i<=count;i++)
260 {
261 ddl.Items.Add(i.ToString());
262
263 }
264 }
265 #endregion
266#region InitEvent
267 private void bt_Click(object sender, EventArgs e)
268 {
269 this.PageSize=Convert.ToInt32(tb.Text);
270 ddl.Items.Clear();
271 initDDL();
this.CurrentPageIndex=0;
272 BindGrid();
273 }
274 private void PrevPAGE_Click(object sender, EventArgs e)
275 {
276 int con=this.CurrentPageIndex;
277 if (this.CurrentPageIndex-1<0)
278 {
279
280
281 }
282 else
283 {
284 this.CurrentPageIndex=con-1;
285 }
286 ddl.SelectedValue=(this.CurrentPageIndex+1).ToString();
287 BindGrid();
288
289 }
290 private void NextPAGE_Click(object sender, EventArgs e)
291 {
292 int con=this.CurrentPageIndex;
293 if (this.CurrentPageIndex>=this.PageCount-1)
294 {
295
296 }
297 else
298 {
299 this.CurrentPageIndex=con+1;
300 }
301 ddl.SelectedValue=(this.CurrentPageIndex+1).ToString();
302 BindGrid();
303
304 }
305 private void ddl_SelectedIndexChanged(object sender, EventArgs e)
306 {
307 int curri=Convert.ToInt16(ddl.SelectedValue) - 1;
308 this.CurrentPageIndex=curri;
309 BindGrid();
310 }
311
312 #endregion
313#region GridFunction
314private void BindGrid()
315{
316 this.DataSource=(DataView)Page.Session[sessionName];
317 this.DataBind();
318
319}
320 #endregion
321
322
323 }
324}
325
2using System.Web.UI;
3using System.Web.UI.WebControls;
4using System.ComponentModel;
5using System.Data;
6
7namespace LcWebControls
8{
9 /// <summary>
10 /// 卢昶 2005-7-11
11 /// Email:darkangel@ynmail.com
12 /// msn:darkangellc@msn.com
13 /// </summary>
14 [DefaultProperty("Text"),
15 ToolboxData("<{0}:LcDataGrid runat=server></{0}:LcDataGrid>")]
16 public class LcDataGrid : System.Web.UI.WebControls.DataGrid
17 {
18#region Define
19 private string text;
20 private System.Web.UI.WebControls.Button bt=new Button();
21 private TextBox tb=new TextBox();
22 private System.Web.UI.Page pg=new Page();
23 private string sessionname;
24 private LinkButton PrevPAGE=new LinkButton();
25 private LinkButton NextPAGE=new LinkButton();
26 private DropDownList ddl=new DropDownList();
27 private Label lbend=new Label();
28 private Label lbtag=new Label();
29 private Label lbddls=new Label();
30 private Label lbddle=new Label();
31
32#endregion
33#region displayPortery
34
35 public int Currindex
36 {
37 set
38 {
39 try
40 {
41 this.CurrentPageIndex=value;
42 }
43 catch
44 {
45
46 }
47
48 }
49 get
50 {
51 return this.CurrentPageIndex;
52 }
53 }
54#endregion
55#region Property
56
57
58 [Bindable(true),
59 Category("Appearance"),
60 DefaultValue("")]
61 public string Text
62 {
63 get
64 {
65 return text;
66 }
67
68 set
69 {
70 text = value;
71 }
72 }
73 [Bindable(true),
74 Category("Appearance"),
75 DefaultValue(""),
76 DescriptionAttribute("Session的名称,Session用于数据绑定,必须是DataView类型")
77 ]
78 public string sessionName
79 {
80 get
81 {
82 return sessionname;
83 }
84 set
85 {
86 sessionname=value;
87
88 }
89 }
90 [Bindable(true),
91 Category("Appearance"),
92 DefaultValue("")]
93 public string TT
94 {
95 get
96 {
97 try
98 {
99
100 return this.ViewState["sort"].ToString();
101 }
102 catch
103 {
104 this.ViewState["sort"]="asc";
105 return this.ViewState["sort"].ToString();
106 }
107
108 }
109 set
110 {
111 this.ViewState["sort"]=value;
112 }
113
114 }
115
116 #endregion
117
118 /// <summary>
119 /// 将此控件呈现给指定的输出参数。
120 /// </summary>
121 /// <param name="output"> 要写出到的 HTML 编写器 </param>
122
123protected override void OnItemCreated(DataGridItemEventArgs e)
124 {
125
126 if(e.Item.ItemType == ListItemType.Pager)
127 {
128 e.Item.Cells[0].Controls.Clear();
129 TextBox tb=new TextBox();
130 creatControl(e.Item.Cells[0]);
131 }
132
133
134 }
135protected override void OnItemCommand(DataGridCommandEventArgs e)
136 {
137 string strCommandName = e.CommandName;
138 if(e.CommandName == null)
139 return;
140
141 strCommandName = string.IsInterned(strCommandName);
142 switch(e.CommandName)
143 {
144
145 case "PrevPAGE":
146 {
147 PrevPAGE_Click(null,null);
148 break;
149 }
150 case "NextPAGE":
151 {
152 NextPAGE_Click(null,null);
153 break;
154 }
155
156 }
157 base.OnItemCommand(e);
158 }
159
160protected override void OnItemDataBound(DataGridItemEventArgs e)
161{
162
163 if(e.Item.ItemType==ListItemType.Item||e.Item.ItemType==ListItemType.AlternatingItem)
164 {
165 e.Item.Attributes.Add("style","cursor:hand");
166 e.Item.Attributes.Add("onmouseover","this.style.backgroundColor='#FFCC00'");
167 e.Item.Attributes.Add("onmouseout","this.style.backgroundColor=''");
168 }
169}
170 protected override void OnSortCommand(DataGridSortCommandEventArgs e)
171 {
172 base.OnSortCommand (e);
173 DataView dv=(DataView)this.Page.Session[sessionName];
174 if (TT.Equals("desc"))
175 {
176 dv.Sort=e.SortExpression+" asc";
177 TT="asc";
178 }
179 else
180 {
181 dv.Sort=e.SortExpression+" desc";
182 TT="desc";
183 }
184
185
186 this.DataSource=dv;
187 this.DataBind();
188 this.Page.Session[sessionName]=dv;
189 }
190
191
192
193
194#region InitDataGird
195
196 protected void creatControl(TableCell tc)
197 {
198
199 creatPageCount(tc);
200 creatCount(tc);
201 creatSPage(tc);
202 creatDDL(tc);
203
204
205 }
206 private void creatCount(TableCell tc)
207 {
208
209 lbtag.Text=" 每页显示记录数";
210 tb.Text=this.PageSize.ToString();
211 tb.Width=20;
212 bt.Text="提交";
213 bt.Click+=new EventHandler(bt_Click);
214 tc.Controls.Add(lbtag);
215 tc.Controls.Add(tb);
216 tc.Controls.Add(bt);
217 //
218
219 }
220
221 private void creatPageCount(TableCell tc)
222 {
223 Label lb=new Label();
224 lb.Text="<P align=\"right\"> 共"+this.PageCount+"页 第"+(this.CurrentPageIndex+1)+"页 ";
225 tc.Controls.Add(lb);
226
227 }
228
229 private void creatSPage(TableCell tc)
230 {
231 PrevPAGE.Text="上一页";
232 NextPAGE.Text="下一页";
233 PrevPAGE.CommandName="PrevPAGE";
234 NextPAGE.CommandName="NextPAGE";
235 tc.Controls.Add(PrevPAGE);
236 tc.Controls.Add(NextPAGE);
237 }
238 private void creatDDL(TableCell tc)
239 {
240
241 lbend.Text="页</p>";
242 lbddls.Text=" 跳转到第";
243 ddl.AutoPostBack=true;
244 ddl.EnableViewState=true;
245 if (!this.Page.IsPostBack)
246 {
247 ddl.Items.Clear();
248 initDDL();
249 }
250 ddl.SelectedIndexChanged+=new EventHandler(ddl_SelectedIndexChanged);
251 tc.Controls.Add(lbddls);
252 tc.Controls.Add(ddl);
253 tc.Controls.Add(lbend);
254 }
255 private void initDDL()
256 {
257 int count=this.PageCount;
258
259 for (int i=1;i<=count;i++)
260 {
261 ddl.Items.Add(i.ToString());
262
263 }
264 }
265 #endregion
266#region InitEvent
267 private void bt_Click(object sender, EventArgs e)
268 {
269 this.PageSize=Convert.ToInt32(tb.Text);
270 ddl.Items.Clear();
271 initDDL();
this.CurrentPageIndex=0;
272 BindGrid();
273 }
274 private void PrevPAGE_Click(object sender, EventArgs e)
275 {
276 int con=this.CurrentPageIndex;
277 if (this.CurrentPageIndex-1<0)
278 {
279
280
281 }
282 else
283 {
284 this.CurrentPageIndex=con-1;
285 }
286 ddl.SelectedValue=(this.CurrentPageIndex+1).ToString();
287 BindGrid();
288
289 }
290 private void NextPAGE_Click(object sender, EventArgs e)
291 {
292 int con=this.CurrentPageIndex;
293 if (this.CurrentPageIndex>=this.PageCount-1)
294 {
295
296 }
297 else
298 {
299 this.CurrentPageIndex=con+1;
300 }
301 ddl.SelectedValue=(this.CurrentPageIndex+1).ToString();
302 BindGrid();
303
304 }
305 private void ddl_SelectedIndexChanged(object sender, EventArgs e)
306 {
307 int curri=Convert.ToInt16(ddl.SelectedValue) - 1;
308 this.CurrentPageIndex=curri;
309 BindGrid();
310 }
311
312 #endregion
313#region GridFunction
314private void BindGrid()
315{
316 this.DataSource=(DataView)Page.Session[sessionName];
317 this.DataBind();
318
319}
320 #endregion
321
322
323 }
324}
325