功能设计
开发:
新建控件项目专案
继承相关接口
创建子控件
公开相关 属性 方法 事件
编译生成
使用:
添加引用
因为时间关系 现只将示例源代码写出
本实例开发的是一个由ListBox和6个ImageButton组成的列表组件
通过ImageButton可以控制列表项的增加和删除
以及列表项在列表中的位置
到第一位 上一位 下一位 到最后位
示例代码
1
using#region using
2
using System;
3
using System.Web.UI;
4
using System.Web.UI.WebControls;
5
using System.ComponentModel;
6
using System.Drawing;
7
using System.Text;
8
using System.Collections.Specialized;
9
#endregion
10
11
namespace JX_CC_LC
12

{
13
/**//// <summary>
14
/// LC_T01 的摘要描述。
15
/// </summary>
16
[DefaultProperty(""),
17
ToolboxData("<{0}:LC_T01 runat=server></{0}:LC_T01>")]
18
public class LC_T01 : System.Web.UI.WebControls.WebControl, INamingContainer//,IPostBackDataHandler
19
{
20
//声明所要用到的ListBox,ImageButton
21
#region
22
private System.Web.UI.WebControls.ListBox lisb_LcT01;
23
private System.Web.UI.WebControls.ImageButton ibt_Add;
24
private System.Web.UI.WebControls.ImageButton ibt_Del;
25
private System.Web.UI.WebControls.ImageButton ibt_First;
26
private System.Web.UI.WebControls.ImageButton ibt_Pre;
27
private System.Web.UI.WebControls.ImageButton ibt_Next;
28
private System.Web.UI.WebControls.ImageButton ibt_Last;
29
#endregion
30
//声明事件
31
#region
32
public event System.EventHandler addOnClick;
33
//public event System.EventHandler iSelectedIndexChanged;
34
#endregion
35
//创建子控件
36
#region
37
protected override void CreateChildControls()
38
{
39
this.Controls.Clear();
40
子控件创建部分#region 子控件创建部分
41
//this.ClearChildViewState();
42
this.Controls.Add(new LiteralControl("<table Border='0' Cellpadding='0' Cellspacing='0' ><tr><td rowspan=7>"));
43
this.lisb_LcT01 = new ListBox();
44
this.lisb_LcT01.Width = 150;
45
this.lisb_LcT01.Height = 200;
46
//this.lisb_LcT01.AutoPostBack=true;
47
//this.lisb_LcT01.SelectedIndexChanged+=new EventHandler(lisb_LcT01_SelectedIndexChanged);
48
this.lisb_LcT01.ID = this.ID + "_ilistbox";
49
this.lisb_LcT01.MergeStyle(this.ControlStyle);
50
this.Controls.Add(this.lisb_LcT01);
51
this.Controls.Add(new LiteralControl("</td></tr><tr><td>"));
52
53
this.ibt_Add = new ImageButton();
54
this.ibt_Add.ImageUrl = "img/addnew.JPG";
55
this.ibt_Add.Click += new ImageClickEventHandler(ibt_Add_Click);
56
this.Controls.Add(this.ibt_Add);
57
this.Controls.Add(new LiteralControl("</td></tr><tr><td>"));
58
59
this.ibt_Del = new ImageButton();
60
this.ibt_Del.ImageUrl = "img/del.JPG";
61
this.ibt_Del.Attributes.Add("onclick", this.ID + "_del_click();return false;");
62
this.Controls.Add(this.ibt_Del);
63
this.Controls.Add(new LiteralControl("</td></tr><tr><td>"));
64
65
this.ibt_First = new ImageButton();
66
this.ibt_First.ImageUrl = "img/first.JPG";
67
this.ibt_First.Attributes.Add("onclick", this.ID + "_first_click();return false;");
68
this.Controls.Add(this.ibt_First);
69
this.Controls.Add(new LiteralControl("</td></tr><tr><td>"));
70
71
this.ibt_Pre = new ImageButton();
72
this.ibt_Pre.ImageUrl = "img/pre.JPG";
73
this.ibt_Pre.Attributes.Add("onclick", this.ID + "_pre_click();return false;");
74
this.Controls.Add(this.ibt_Pre);
75
this.Controls.Add(new LiteralControl("</td></tr><tr><td>"));
76
77
this.ibt_Next = new ImageButton();
78
this.ibt_Next.ImageUrl = "img/next.JPG";
79
this.ibt_Next.Attributes.Add("onclick", this.ID + "_next_click();return false;");
80
this.Controls.Add(this.ibt_Next);
81
this.Controls.Add(new LiteralControl("</td></tr><tr><td>"));
82
83
this.ibt_Last = new ImageButton();
84
this.ibt_Last.ImageUrl = "img/last.JPG";
85
this.ibt_Last.Attributes.Add("onclick", this.ID + "_last_click();return false;");
86
this.Controls.Add(this.ibt_Last);
87
this.Controls.Add(new LiteralControl("</td></tr></table>"));
88
#endregion
89
//JS 部分
90
#region
91
//del
92
System.Text.StringBuilder strbuild = new StringBuilder();
93
strbuild.Append("<script language='javascript'>");
94
strbuild.Append(" function " + this.ID + "_del_click(){");
95
strbuild.Append(" var i=document.all." + this.ID + "_" + this.ID + "_ilistbox.options.length;");
96
strbuild.Append(" var j=document.all." + this.ID + "_" + this.ID + "_ilistbox.selectedIndex;");
97
strbuild.Append(" if(i>0&&j>=0){");
98
strbuild.Append(" document.all." + this.ID + "_" + this.ID + "_ilistbox.remove(j);} ");
99
strbuild.Append("} </script>");
100
//first
101
strbuild.Append("<script language='javascript'>");
102
strbuild.Append(" function " + this.ID + "_first_click(){");
103
strbuild.Append(" var i=document.all." + this.ID + "_" + this.ID + "_ilistbox.options.length;");
104
strbuild.Append(" var j=document.all." + this.ID + "_" + this.ID + "_ilistbox.selectedIndex;");
105
strbuild.Append(" if(i>1&&j>0){");
106
strbuild.Append(" var tempValue=document.all." + this.ID + "_" + this.ID + "_ilistbox.options[j].value;");
107
strbuild.Append(" var tempText=document.all." + this.ID + "_" + this.ID + "_ilistbox.options[j].text;");
108
strbuild.Append(" for(var h=j;h>0;h--){");
109
strbuild.Append(" document.all." + this.ID + "_" + this.ID + "_ilistbox.options[h].value=document.all." + this.ID + "_" + this.ID + "_ilistbox.options[h-1].value;");
110
strbuild.Append(" document.all." + this.ID + "_" + this.ID + "_ilistbox.options[h].text=document.all." + this.ID + "_" + this.ID + "_ilistbox.options[h-1].text;");
111
strbuild.Append(" }");
112
strbuild.Append(" document.all." + this.ID + "_" + this.ID + "_ilistbox.options[0].value=tempValue;");
113
strbuild.Append(" document.all." + this.ID + "_" + this.ID + "_ilistbox.options[0].text=tempText;");
114
strbuild.Append(" document.all." + this.ID + "_" + this.ID + "_ilistbox.selectedIndex=0;");
115
strbuild.Append("} }</script>");
116
//pre
117
strbuild.Append("<script language='javascript'>");
118
strbuild.Append(" function " + this.ID + "_pre_click(){");
119
strbuild.Append(" var i=document.all." + this.ID + "_" + this.ID + "_ilistbox.options.length;");
120
strbuild.Append(" var j=document.all." + this.ID + "_" + this.ID + "_ilistbox.selectedIndex;");
121
strbuild.Append(" if(i>1&&j>0){");
122
strbuild.Append(" var tempValue=document.all." + this.ID + "_" + this.ID + "_ilistbox.options[j].value;");
123
strbuild.Append(" var tempText=document.all." + this.ID + "_" + this.ID + "_ilistbox.options[j].text;");
124
strbuild.Append(" document.all." + this.ID + "_" + this.ID + "_ilistbox.options[j].value=document.all." + this.ID + "_" + this.ID + "_ilistbox.options[j-1].value;");
125
strbuild.Append(" document.all." + this.ID + "_" + this.ID + "_ilistbox.options[j].text=document.all." + this.ID + "_" + this.ID + "_ilistbox.options[j-1].text;");
126
strbuild.Append(" document.all." + this.ID + "_" + this.ID + "_ilistbox.options[j-1].value=tempValue;");
127
strbuild.Append(" document.all." + this.ID + "_" + this.ID + "_ilistbox.options[j-1].text=tempText;");
128
strbuild.Append(" document.all." + this.ID + "_" + this.ID + "_ilistbox.selectedIndex=j-1;");
129
strbuild.Append("} }</script>");
130
//next
131
strbuild.Append("<script language='javascript'>");
132
strbuild.Append(" function " + this.ID + "_next_click(){");
133
strbuild.Append(" var i=document.all." + this.ID + "_" + this.ID + "_ilistbox.options.length;");
134
strbuild.Append(" var j=document.all." + this.ID + "_" + this.ID + "_ilistbox.selectedIndex;");
135
strbuild.Append(" if(i>1&&j<i-1){");
136
strbuild.Append(" var tempValue=document.all." + this.ID + "_" + this.ID + "_ilistbox.options[j].value;");
137
strbuild.Append(" var tempText=document.all." + this.ID + "_" + this.ID + "_ilistbox.options[j].text;");
138
strbuild.Append(" document.all." + this.ID + "_" + this.ID + "_ilistbox.options[j].value=document.all." + this.ID + "_" + this.ID + "_ilistbox.options[j+1].value;");
139
strbuild.Append(" document.all." + this.ID + "_" + this.ID + "_ilistbox.options[j].text=document.all." + this.ID + "_" + this.ID + "_ilistbox.options[j+1].text;");
140
strbuild.Append(" document.all." + this.ID + "_" + this.ID + "_ilistbox.options[j+1].value=tempValue;");
141
strbuild.Append(" document.all." + this.ID + "_" + this.ID + "_ilistbox.options[j+1].text=tempText;");
142
strbuild.Append(" document.all." + this.ID + "_" + this.ID + "_ilistbox.selectedIndex=j+1;");
143
strbuild.Append("} }</script>");
144
//last
145
strbuild.Append("<script language='javascript'>");
146
strbuild.Append(" function " + this.ID + "_last_click(){");
147
strbuild.Append(" var i=document.all." + this.ID + "_" + this.ID + "_ilistbox.options.length;");
148
strbuild.Append(" var j=document.all." + this.ID + "_" + this.ID + "_ilistbox.selectedIndex;");
149
strbuild.Append(" if(i>1&&j<i-1){");
150
strbuild.Append(" var tempValue=document.all." + this.ID + "_" + this.ID + "_ilistbox.options[j].value;");
151
strbuild.Append(" var tempText=document.all." + this.ID + "_" + this.ID + "_ilistbox.options[j].text;");
152
strbuild.Append(" for(var h=j;h<i-1;h++){");
153
strbuild.Append(" document.all." + this.ID + "_" + this.ID + "_ilistbox.options[h].value=document.all." + this.ID + "_" + this.ID + "_ilistbox.options[h+1].value;");
154
strbuild.Append(" document.all." + this.ID + "_" + this.ID + "_ilistbox.options[h].text=document.all." + this.ID + "_" + this.ID + "_ilistbox.options[h+1].text;");
155
strbuild.Append("}");
156
strbuild.Append(" document.all." + this.ID + "_" + this.ID + "_ilistbox.options[i-1].value=tempValue;");
157
strbuild.Append(" document.all." + this.ID + "_" + this.ID + "_ilistbox.options[i-1].text=tempText;");
158
strbuild.Append(" document.all." + this.ID + "_" + this.ID + "_ilistbox.selectedIndex=i-1;");
159
strbuild.Append("} }</script>");
160
this.Controls.Add(new LiteralControl(strbuild.ToString()));
161
#endregion
162
base.CreateChildControls();
163
164
}
165
#endregion
166
//输出
167
#region
168
protected override void Render(HtmlTextWriter writer)
169
{
170
this.EnsureChildControls();
171
base.Render(writer);
172
}
173
#endregion
174
//图像来源控制
175
#region
176
//add
177
[System.ComponentModel.Bindable(true),
178
System.ComponentModel.Category("Appearance"),
179
System.ComponentModel.DefaultValue("img/addnew.JPG")]
180
public string add_src
181
{
182
get
183
{
184
this.EnsureChildControls();
185
return ((ImageButton)Controls[3]).ImageUrl.ToString();
186
187
}
188
set
189
{
190
this.EnsureChildControls();
191
((ImageButton)Controls[3]).ImageUrl = value;
192
}
193
}
194
//del
195
[Bindable(false),
196
Category("Appearance"),
197
DefaultValue("img/del.JPG")]
198
public string del_src
199
{
200
get
201
{
202
this.EnsureChildControls();
203
return ((ImageButton)Controls[5]).ImageUrl;
204
205
}
206
set
207
{
208
this.EnsureChildControls();
209
((ImageButton)Controls[5]).ImageUrl = value;
210
}
211
}
212
//first
213
public string first_src
214
{
215
get
216
{
217
this.EnsureChildControls();
218
return ((ImageButton)Controls[5]).ImageUrl;
219
220
}
221
set
222
{
223
this.EnsureChildControls();
224
((ImageButton)Controls[5]).ImageUrl = value;
225
}
226
}
227
//pre
228
public string pre_src
229
{
230
get
231
{
232
this.EnsureChildControls();
233
return ((ImageButton)Controls[9]).ImageUrl;
234
}
235
set
236
{
237
this.EnsureChildControls();
238
((ImageButton)Controls[9]).ImageUrl = value;
239
}
240
}
241
//next
242
public string next_src
243
{
244
get
245
{
246
this.EnsureChildControls();
247
return ((ImageButton)Controls[11]).ImageUrl;
248
}
249
set
250
{
251
this.EnsureChildControls();
252
((ImageButton)Controls[11]).ImageUrl = value;
253
}
254
}
255
//last
256
public string last_src
257
{
258
get
259
{
260
this.EnsureChildControls();
261
return ((ImageButton)Controls[13]).ImageUrl;
262
}
263
set
264
{
265
this.EnsureChildControls();
266
((ImageButton)Controls[13]).ImageUrl = value;
267
268
}
269
}
270
#endregion
271
//ADD 按键事件
272
#region
273
protected virtual void ibt_Add_Click(object sender, System.Web.UI.ImageClickEventArgs e)
274
{
275
276
EventArgs e_add = new EventArgs();
277
if (this.addOnClick != null)
278
this.addOnClick(this.ibt_Add, e_add);
279
280
}
281
#endregion
282
控件属性部分#region 控件属性部分
283
//iDataSource
284
public object iDataSource
285
{
286
get
287
{
288
this.EnsureChildControls();
289
return ((ListBox)Controls[1]).DataSource;
290
}
291
set
292
{
293
this.EnsureChildControls();
294
((ListBox)Controls[1]).DataSource = value;
295
}
296
}
297
//DataBind
298
public void iDataBind()
299
{
300
this.EnsureChildControls();
301
((ListBox)Controls[1]).DataBind();
302
}
303
public string iDataTextField
304
{
305
get
306
{
307
this.EnsureChildControls();
308
return ((ListBox)Controls[1]).DataTextField;
309
}
310
set
311
{
312
this.EnsureChildControls();
313
((ListBox)Controls[1]).DataTextField = value;
314
}
315
316
}
317
public string iDataValueField
318
{
319
get
320
{
321
this.EnsureChildControls();
322
return ((ListBox)Controls[1]).DataValueField;
323
}
324
set
325
{
326
this.EnsureChildControls();
327
((ListBox)Controls[1]).DataValueField = value;
328
}
329
330
}
331
//得到目前的索引
332
public int iSelectIndex()
333
{
334
this.EnsureChildControls();
335
int i = ((ListBox)Controls[1]).SelectedIndex;
336
return i;
337
}
338
//得到ITEM的个数
339
public int iCountItem()
340
{
341
this.EnsureChildControls();
342
int i = ((ListBox)Controls[1]).Items.Count;
343
return i;
344
}
345
//添加一个ITEM 到LISTBOX
346
public void iAddItem(string addText, string addValue)
347
{
348
this.EnsureChildControls();
349
ListItem temp = new ListItem();
350
temp.Text = addText.Trim().ToString();
351
temp.Value = addValue.Trim().ToString();
352
((ListBox)Controls[1]).Items.Add(temp);
353
354
}
355
public void iAddItem(string addText)
356
{
357
this.EnsureChildControls();
358
ListItem temp = new ListItem();
359
temp.Text = addText.Trim().ToString();
360
((ListBox)Controls[1]).Items.Add(temp);
361
362
}
363
364
365
//返回当前所选的Item项
366
public ListItem iSelectItem()
367
{
368
this.EnsureChildControls();
369
int index = ((ListBox)Controls[1]).SelectedIndex;
370
int i = ((ListBox)Controls[1]).Items.Count;
371
if (index >= 0 && index < i)
372
{
373
return ((ListBox)Controls[1]).Items[index];
374
}
375
else
376
{
377
return null;
378
}
379
}
380
//选择LISTBOX中第index项
381
public ListItem iSelectItem(int index)
382
{
383
this.EnsureChildControls();
384
int i = ((ListBox)Controls[1]).Items.Count;
385
if (index >= 0 && index < i)
386
{
387
return ((ListBox)Controls[1]).Items[index];
388
}
389
else
390
{
391
return null;
392
}
393
}
394
//清空LISTBOX
395
public void iClearItem()
396
{
397
this.EnsureChildControls();
398
((ListBox)Controls[1]).Items.Clear();
399
}
400
//删除目前的所选值
401
public void iRemoveItem()
402
{
403
this.EnsureChildControls();
404
int i = ((ListBox)Controls[1]).SelectedIndex;
405
if (i >= 0)
406
{
407
((ListBox)Controls[1]).Items.Remove(((ListBox)Controls[1]).Items[i]);
408
}
409
}
410
public void iRemoveAtItem(int index)
411
{
412
this.EnsureChildControls();
413
int i = ((ListBox)Controls[1]).Items.Count;
414
if (index > 0 && index < i)
415
{
416
((ListBox)Controls[1]).Items.Remove(((ListBox)Controls[1]).Items[index]);
417
}
418
}
419
//重载宽度
420
#region
421
public override Unit Width
422
{
423
get
424
{
425
return base.Width;
426
}
427
set
428
{
429
base.Width = value;
430
this.EnsureChildControls();
431
((ListBox)Controls[1]).Width = System.Convert.ToInt32(value.Value * 0.9);
432
}
433
}
434
//重载高度
435
public override Unit Height
436
{
437
get
438
{
439
return base.Height;
440
}
441
set
442
{
443
base.Height = value;
444
this.EnsureChildControls();
445
((ListBox)Controls[1]).Height = System.Convert.ToInt32(value.Value * 0.98);
446
}
447
}
448
#endregion
449
#endregion
450
}//class
451
}//namespace
452


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

173

174

175



176

177

178

179

180

181



182

183



184

185

186

187

188

189



190

191

192

193

194

195

196

197

198

199



200

201



202

203

204

205

206

207



208

209

210

211

212

213

214



215

216



217

218

219

220

221

222



223

224

225

226

227

228

229



230

231



232

233

234

235

236



237

238

239

240

241

242

243



244

245



246

247

248

249

250



251

252

253

254

255

256

257



258

259



260

261

262

263

264



265

266

267

268

269

270

271

272



273

274



275

276

277

278

279

280

281

282


283

284

285



286

287



288

289

290

291

292



293

294

295

296

297

298

299



300

301

302

303

304



305

306



307

308

309

310

311



312

313

314

315

316

317

318



319

320



321

322

323

324

325



326

327

328

329

330

331

332

333



334

335

336

337

338

339

340



341

342

343

344

345

346

347



348

349

350

351

352

353

354

355

356



357

358

359

360

361

362

363

364

365

366

367



368

369

370

371

372



373

374

375

376



377

378

379

380

381

382



383

384

385

386



387

388

389

390



391

392

393

394

395

396



397

398

399

400

401

402



403

404

405

406



407

408

409

410

411



412

413

414

415



416

417

418

419

420



421

422



423

424



425

426

427

428



429

430

431

432

433

434

435

436



437

438



439

440

441

442



443

444

445

446

447

448

449

450

451

452
