1
|
|
#region Copyright
|
2
|
|
|
3
|
|
|
4
|
|
|
5
|
|
|
6
|
|
|
7
|
|
|
8
|
|
|
9
|
|
|
10
|
|
|
11
|
|
|
12
|
|
|
13
|
|
|
14
|
|
|
15
|
|
|
16
|
|
|
17
|
|
|
18
|
|
|
19
|
|
|
20
|
|
|
21
|
|
|
22
|
|
#endregion
|
23
|
|
|
24
|
|
using System;
|
25
|
|
using System.Collections;
|
26
|
|
using System.ComponentModel;
|
27
|
|
using System.Reflection;
|
28
|
|
using System.Web.UI;
|
29
|
|
using System.Web.UI.WebControls;
|
30
|
|
using ShowX.Config;
|
31
|
|
using ShowX.Web.WebBuilders;
|
32
|
|
|
33
|
|
namespace ShowX.Web.WebControls
|
34
|
|
{
|
35
|
|
|
36
|
|
|
37
|
|
|
38
|
|
[Flags]
|
39
|
|
public enum DataGridOps {
|
40
|
|
|
41
|
|
|
42
|
|
|
43
|
|
|
44
|
|
None = 0,
|
45
|
|
|
46
|
|
|
47
|
|
|
48
|
|
InplaceEditing = 2,
|
49
|
|
|
50
|
|
|
51
|
|
|
52
|
|
Deletion = 4,
|
53
|
|
|
54
|
|
|
55
|
|
|
56
|
|
All = 8
|
57
|
|
}
|
58
|
|
|
59
|
|
|
60
|
|
|
61
|
|
|
62
|
|
public delegate void OnDeleteElemEventHandler(object elem);
|
63
|
|
|
64
|
|
|
65
|
|
|
66
|
|
|
67
|
|
|
68
|
|
|
69
|
|
[DefaultProperty("Text"),
|
70
|
|
ToolboxData("<{0}:DataGrid runat=server></{0}:DataGrid>")]
|
71
|
|
public class DataGrid : System.Web.UI.WebControls.DataGrid
|
72
|
|
{
|
73
|
|
|
74
|
|
|
75
|
|
|
76
|
|
|
77
|
|
public event OnDeleteElemEventHandler OnDeleteElem;
|
78
|
|
|
79
|
|
#region Protected methods and variables
|
80
|
|
|
81
|
|
TableItemStyle childControlStyle;
|
82
|
|
|
83
|
|
|
84
|
|
|
85
|
|
|
86
|
|
protected ArrayList dataSource;
|
87
|
|
|
88
|
|
|
89
|
|
|
90
|
|
|
91
|
|
protected object context;
|
92
|
|
|
93
|
|
|
94
|
|
|
95
|
|
|
96
|
|
|
97
|
|
|
98
|
|
|
99
|
|
|
100
|
|
|
101
|
|
|
102
|
56
|
protected void dgSort (string SortExpression)
|
103
|
|
{
|
104
|
56
|
if (SortExpression == null) return;
|
105
|
|
|
106
|
27
|
PropertyInfo pInfo = Type.GetProperty(SortExpression);
|
107
|
|
|
108
|
0
|
if (pInfo == null) return;
|
109
|
|
|
110
|
27
|
dataSource.Sort(new CmpByProperty(pInfo,InvertSort));
|
111
|
|
}
|
112
|
|
|
113
|
|
#endregion
|
114
|
|
|
115
|
|
|
116
|
|
|
117
|
|
|
118
|
35
|
public DataGrid()
|
119
|
|
{
|
120
|
35
|
EditItemIndex = -1;
|
121
|
35
|
AutoGenerateColumns = false;
|
122
|
35
|
InvertSort = false;
|
123
|
35
|
AllowPaging = true;
|
124
|
35
|
AllowSorting = true;
|
125
|
|
}
|
126
|
|
|
127
|
|
|
128
|
|
|
129
|
|
|
130
|
|
[Bindable(true), Category("Business"), DefaultValue("")]
|
131
|
|
protected Type Type
|
132
|
|
{
|
133
|
160
|
get
|
134
|
|
{
|
135
|
160
|
return (Type)(ViewState["ShowXObjectBrowserType"]);
|
136
|
|
}
|
137
|
|
|
138
|
9
|
set
|
139
|
|
{
|
140
|
9
|
ViewState["ShowXObjectBrowserType"] = value;
|
141
|
9
|
this.ChildControlsCreated = false;
|
142
|
9
|
this.CurrentPageIndex = 0;
|
143
|
9
|
this.EditItemIndex = -1;
|
144
|
9
|
this.CreateChildControls();
|
145
|
|
}
|
146
|
|
}
|
147
|
|
|
148
|
|
|
149
|
|
|
150
|
|
|
151
|
|
|
152
|
|
|
153
|
|
|
154
|
|
|
155
|
|
|
156
|
|
|
157
|
|
|
158
|
|
|
159
|
|
|
160
|
|
|
161
|
9
|
public void BuildDataGrid(Type type)
|
162
|
|
{
|
163
|
9
|
this.Type = type;
|
164
|
|
}
|
165
|
|
|
166
|
|
|
167
|
|
|
168
|
|
|
169
|
|
|
170
|
|
|
171
|
0
|
public void BuildDataGrid(Type type, object context)
|
172
|
|
{
|
173
|
|
this.context = context;
|
174
|
|
this.Type = type;
|
175
|
|
}
|
176
|
|
|
177
|
|
|
178
|
|
|
179
|
|
|
180
|
|
[Bindable(true), Category("Business"), DefaultValue(DataGridOps.All)]
|
181
|
|
public DataGridOps DataGridOps
|
182
|
|
{
|
183
|
459
|
get
|
184
|
|
{
|
185
|
459
|
return (ViewState["ShowXObjectBrowserDataGridOps"]==null)
|
186
|
|
? DataGridOps.All
|
187
|
|
:(DataGridOps)(ViewState["ShowXObjectBrowserDataGridOps"]);
|
188
|
|
}
|
189
|
0
|
set { ViewState["ShowXObjectBrowserDataGridOps"] = value; }
|
190
|
|
}
|
191
|
|
|
192
|
|
|
193
|
|
|
194
|
|
|
195
|
|
|
196
|
|
public object XContext
|
197
|
|
{
|
198
|
77
|
get
|
199
|
|
{
|
200
|
77
|
return this.context;
|
201
|
|
}
|
202
|
|
}
|
203
|
|
|
204
|
|
|
205
|
|
|
206
|
|
|
207
|
|
[Bindable(true), Category("Business"), DefaultValue("")]
|
208
|
|
public string DateTimeFormat
|
209
|
|
{
|
210
|
221
|
get {
|
211
|
221
|
return (ViewState["ShowXObjectBrowserDateTimeFormat"] == null)
|
212
|
|
? ""
|
213
|
|
:(string)(ViewState["ShowXObjectBrowserDateTimeFormat"]);
|
214
|
|
}
|
215
|
25
|
set { ViewState["ShowXObjectBrowserDateTimeFormat"] = value; }
|
216
|
|
}
|
217
|
|
|
218
|
|
|
219
|
|
|
220
|
|
|
221
|
|
[Bindable(true), Category("Business"), DefaultValue("")]
|
222
|
|
public string ActiveView
|
223
|
|
{
|
224
|
35
|
get
|
225
|
|
{
|
226
|
35
|
return (ViewState["ShowXObjectBrowserActiveView"] == null)
|
227
|
|
? string.Empty
|
228
|
|
: (string)(ViewState["ShowXObjectBrowserActiveView"]);
|
229
|
|
}
|
230
|
0
|
set { ViewState["ShowXObjectBrowserActiveView"] = value; }
|
231
|
|
}
|
232
|
|
|
233
|
|
|
234
|
|
|
235
|
|
|
236
|
|
[Bindable(true), Category("Style"), Browsable(true)]
|
237
|
|
[PersistenceMode(PersistenceMode.InnerProperty),
|
238
|
|
TemplateContainer(typeof(AdminElement))]
|
239
|
|
public TableItemStyle ChildControlStyle
|
240
|
|
{
|
241
|
146
|
get
|
242
|
|
{
|
243
|
146
|
if (childControlStyle == null)
|
244
|
0
|
childControlStyle = new TableItemStyle();
|
245
|
|
|
246
|
146
|
return childControlStyle;
|
247
|
|
}
|
248
|
35
|
set
|
249
|
|
{
|
250
|
35
|
childControlStyle = value;
|
251
|
|
}
|
252
|
|
}
|
253
|
|
|
254
|
|
|
255
|
|
|
256
|
|
|
257
|
|
public string SortExpression
|
258
|
|
{
|
259
|
67
|
get
|
260
|
|
{
|
261
|
67
|
return (string)(ViewState["ShowXObjectBrowserSortExpression"]);
|
262
|
|
}
|
263
|
|
|
264
|
11
|
set
|
265
|
|
{
|
266
|
11
|
ViewState["ShowXObjectBrowserSortExpression"] = value;
|
267
|
|
}
|
268
|
|
}
|
269
|
|
|
270
|
|
|
271
|
|
|
272
|
|
|
273
|
|
|
274
|
|
|
275
|
|
public bool InvertSort
|
276
|
|
{
|
277
|
33
|
get {
|
278
|
33
|
return (bool)(ViewState["ShowXObjectBrowserInvertSort"]);
|
279
|
|
}
|
280
|
|
|
281
|
81
|
set {
|
282
|
81
|
ViewState["ShowXObjectBrowserInvertSort"] = value;
|
283
|
|
}
|
284
|
|
}
|
285
|
|
|
286
|
|
|
287
|
|
|
288
|
|
|
289
|
35
|
protected override void CreateChildControls()
|
290
|
|
{
|
291
|
0
|
if ((Type == null) || this.ChildControlsCreated) return;
|
292
|
|
|
293
|
35
|
DataGridBuilder dgBuilder = new DataGridBuilder(Type,this);
|
294
|
35
|
this.Columns.Clear();
|
295
|
|
|
296
|
35
|
dgBuilder.BuildColumns();
|
297
|
35
|
ChildControlsCreated = true;
|
298
|
|
|
299
|
35
|
this.ItemCreated += new DataGridItemEventHandler(dg_ItemCreated);
|
300
|
35
|
this.CancelCommand += new DataGridCommandEventHandler(this.dg_CancelCommand);
|
301
|
35
|
this.EditCommand += new DataGridCommandEventHandler(this.dg_EditCommand);
|
302
|
35
|
this.UpdateCommand += new DataGridCommandEventHandler(this.dg_UpdateCommand);
|
303
|
35
|
this.PageIndexChanged += new DataGridPageChangedEventHandler(dg_PageIndexChanged);
|
304
|
35
|
this.SortCommand += new DataGridSortCommandEventHandler(dg_SortCommand);
|
305
|
35
|
this.DeleteCommand += new DataGridCommandEventHandler(dg_DeleteCommand);
|
306
|
|
|
307
|
35
|
DataBind();
|
308
|
|
}
|
309
|
|
|
310
|
|
|
311
|
|
|
312
|
|
|
313
|
56
|
public override void DataBind()
|
314
|
|
{
|
315
|
56
|
dataSource = Configuration.Session.MethodCaller.InvokeMethodGetAllX(Type,
|
316
|
|
XContext);
|
317
|
|
|
318
|
56
|
this.DataSource = dataSource;
|
319
|
56
|
dgSort(SortExpression);
|
320
|
56
|
base.DataBind();
|
321
|
|
}
|
322
|
|
|
323
|
|
|
324
|
|
|
325
|
|
#region DataGrid event handlers
|
326
|
|
|
327
|
2
|
private void dg_UpdateCommand(object source, DataGridCommandEventArgs e)
|
328
|
|
{
|
329
|
2
|
ArrayList values = new ArrayList();
|
330
|
2
|
bool validated = true;
|
331
|
|
|
332
|
2
|
int dbpk = int.Parse((e.Item.Cells[0].Controls[0] as Literal).Text);
|
333
|
|
|
334
|
2
|
values.Add(dbpk);
|
335
|
|
|
336
|
20
|
for (int i=1; i < e.Item.Cells.Count; i++) {
|
337
|
|
|
338
|
0
|
if (e.Item.Cells[i].Controls.Count == 0) continue;
|
339
|
|
|
340
|
18
|
if (e.Item.Cells[i].Controls[0] is TextField) {
|
341
|
|
|
342
|
4
|
TextField tf = e.Item.Cells[i].Controls[0] as TextField;
|
343
|
|
|
344
|
4
|
if (tf.Validate(XContext)) {
|
345
|
3
|
values.Add(tf.InputText);
|
346
|
|
}
|
347
|
|
else
|
348
|
1
|
validated = false;
|
349
|
|
|
350
|
|
}
|
351
|
14
|
else if (e.Item.Cells[i].Controls[0] is CheckBox)
|
352
|
2
|
values.Add((e.Item.Cells[i].Controls[0] as CheckBox).Checked);
|
353
|
12
|
else if (e.Item.Cells[i].Controls[0] is ChangePassword) {
|
354
|
|
|
355
|
2
|
ChangePassword cp = e.Item.Cells[i].Controls[0] as ChangePassword;
|
356
|
|
|
357
|
2
|
if (cp.Validate(XContext))
|
358
|
1
|
values.Add(cp.Password);
|
359
|
|
else
|
360
|
1
|
validated = false;
|
361
|
|
}
|
362
|
10
|
else if (e.Item.Cells[i].Controls[0] is
|
363
|
|
ShowX.Web.WebControls.CheckBoxList) {
|
364
|
|
|
365
|
2
|
ShowX.Web.WebControls.CheckBoxList cblist = e.Item.Cells[i]
|
366
|
|
.Controls[0] as ShowX.Web.WebControls.CheckBoxList;
|
367
|
2
|
ArrayList sel = new ArrayList();
|
368
|
|
|
369
|
2
|
foreach (ListItem li in cblist.Items) {
|
370
|
|
|
371
|
6
|
if (li.Selected)
|
372
|
2
|
sel.Add(Configuration.Session.MethodCaller
|
373
|
|
.InvokeMethodGetXByID(cblist.CollectionMap
|
374
|
|
.ContainedType,XContext,int.Parse(li.Value)));
|
375
|
|
}
|
376
|
|
|
377
|
2
|
if (cblist.Validate(XContext))
|
378
|
1
|
values.Add(sel);
|
379
|
|
else
|
380
|
1
|
validated = false;
|
381
|
|
}
|
382
|
8
|
else if (e.Item.Cells[i].Controls[0] is ShowX.Web.WebControls
|
383
|
|
.Calendar) {
|
384
|
|
|
385
|
2
|
values.Add((e.Item.Cells[i].Controls[0] as ShowX.Web.WebControls
|
386
|
|
.Calendar).SelectedDate);
|
387
|
|
}
|
388
|
6
|
else if ((e.Item.Cells[i].Controls.Count == 2)
|
389
|
|
&& (e.Item.Cells[i].Controls[0] is DropDownList)) {
|
390
|
|
|
391
|
|
|
392
|
0
|
DropDownList ddl = e.Item.Cells[i].Controls[0] as DropDownList;
|
393
|
0
|
Literal fkPropertyName = e.Item.Cells[i].Controls[1] as Literal;
|
394
|
|
|
395
|
0
|
int selfkId = int.Parse(ddl.SelectedValue);
|
396
|
|
|
397
|
0
|
PropertyInfo pInfo = Configuration.Session.MappingHandler
|
398
|
|
.GetFK(Type,fkPropertyName.Text);
|
399
|
|
|
400
|
0
|
object fkInstance = Configuration.Session.MethodCaller
|
401
|
|
.InvokeMethodGetXByID(pInfo.PropertyType,XContext,selfkId);
|
402
|
|
|
403
|
0
|
values.Add(fkInstance);
|
404
|
|
}
|
405
|
|
}
|
406
|
|
|
407
|
2
|
if (validated) {
|
408
|
|
|
409
|
1
|
Configuration.Session.MethodCaller
|
410
|
|
.InvokeMethodGetUpdateX(Type,XContext, values.ToArray());
|
411
|
1
|
this.EditItemIndex = -1;
|
412
|
1
|
DataBind();
|
413
|
|
}
|
414
|
|
}
|
415
|
|
|
416
|
0
|
private void dg_CancelCommand(object source, DataGridCommandEventArgs e)
|
417
|
|
{
|
418
|
|
this.EditItemIndex = -1;
|
419
|
|
DataBind();
|
420
|
|
}
|
421
|
|
|
422
|
2
|
private void dg_EditCommand(Object sender, DataGridCommandEventArgs e)
|
423
|
|
{
|
424
|
2
|
this.EditItemIndex = e.Item.ItemIndex;
|
425
|
2
|
DataBind();
|
426
|
|
}
|
427
|
|
|
428
|
11
|
private void dg_SortCommand(object source, DataGridSortCommandEventArgs e)
|
429
|
|
{
|
430
|
11
|
if (SortExpression == e.SortExpression) {
|
431
|
|
|
432
|
6
|
InvertSort = !InvertSort;
|
433
|
|
}
|
434
|
|
else {
|
435
|
|
|
436
|
5
|
InvertSort = false;
|
437
|
|
}
|
438
|
|
|
439
|
11
|
SortExpression = e.SortExpression;
|
440
|
|
|
441
|
11
|
DataBind();
|
442
|
|
}
|
443
|
|
|
444
|
1
|
private void dg_PageIndexChanged(object source, DataGridPageChangedEventArgs e)
|
445
|
|
{
|
446
|
1
|
this.CurrentPageIndex = e.NewPageIndex;
|
447
|
1
|
this.EditItemIndex = -1;
|
448
|
|
|
449
|
1
|
DataBind();
|
450
|
|
}
|
451
|
|
|
452
|
354
|
private void dg_ItemCreated(object sender, DataGridItemEventArgs e)
|
453
|
|
{
|
454
|
354
|
if ((((this.DataGridOps & DataGridOps.All) != 0) ||
|
455
|
|
((this.DataGridOps & DataGridOps.Deletion) != 0))
|
456
|
|
&& (e.Item.ItemType == ListItemType.Item
|
457
|
|
|| e.Item.ItemType == ListItemType.AlternatingItem)) {
|
458
|
|
|
459
|
126
|
WebControl wc = e.Item.Cells[this.Columns.Count-1].Controls[0] as WebControl;
|
460
|
126
|
wc.Attributes.Add("onclick",
|
461
|
|
"return confirm(\"Are you sure you want to remove element?\");");
|
462
|
|
}
|
463
|
|
}
|
464
|
|
|
465
|
6
|
private void dg_DeleteCommand(object source, DataGridCommandEventArgs e)
|
466
|
|
{
|
467
|
6
|
int dbpk = int.Parse((e.Item.Cells[0].Controls[0] as Literal).Text);
|
468
|
|
|
469
|
|
|
470
|
6
|
if ((this.PageSize == 1) || (e.Item.DataSetIndex + 1) % this.PageSize == 1)
|
471
|
5
|
this.CurrentPageIndex = Math.Max(0,CurrentPageIndex-1);
|
472
|
|
|
473
|
6
|
Configuration.Session.MethodCaller.InvokeMethodDelX(Type,XContext,dbpk);
|
474
|
|
|
475
|
6
|
DataBind();
|
476
|
|
|
477
|
6
|
if (this.OnDeleteElem != null)
|
478
|
0
|
OnDeleteElem(null);
|
479
|
|
}
|
480
|
|
|
481
|
|
|
482
|
|
#endregion
|
483
|
|
|
484
|
|
}
|
485
|
|
|
486
|
|
#region Helper classes
|
487
|
|
|
488
|
|
|
489
|
|
|
490
|
|
|
491
|
|
|
492
|
|
public class CmpByProperty : IComparer
|
493
|
|
{
|
494
|
|
|
495
|
|
|
496
|
|
|
497
|
|
PropertyInfo propertyInfo;
|
498
|
|
|
499
|
|
|
500
|
|
|
501
|
|
|
502
|
|
bool invertSort;
|
503
|
|
|
504
|
|
|
505
|
|
|
506
|
|
|
507
|
|
|
508
|
|
|
509
|
|
|
510
|
27
|
public CmpByProperty(PropertyInfo propertyInfo, bool invertSort)
|
511
|
|
{
|
512
|
27
|
this.propertyInfo = propertyInfo;
|
513
|
27
|
this.invertSort = invertSort;
|
514
|
|
}
|
515
|
|
|
516
|
|
|
517
|
|
|
518
|
|
|
519
|
|
|
520
|
|
|
521
|
|
|
522
|
|
|
523
|
|
|
524
|
|
|
525
|
|
|
526
|
428
|
public int Compare(object o1, object o2)
|
527
|
|
{
|
528
|
428
|
object result1 = propertyInfo.GetValue(o1,null);
|
529
|
428
|
object result2 = propertyInfo.GetValue(o2,null);
|
530
|
428
|
int result = 0;
|
531
|
|
|
532
|
428
|
if (propertyInfo.PropertyType is IComparable) {
|
533
|
|
|
534
|
0
|
result = ((IComparable)result1).CompareTo((IComparable)result2);
|
535
|
|
}
|
536
|
|
else {
|
537
|
|
|
538
|
428
|
switch(propertyInfo.PropertyType.ToString()) {
|
539
|
|
|
540
|
|
case "System.String":
|
541
|
156
|
result = ((String)result1).CompareTo((String)result2);
|
542
|
156
|
break;
|
543
|
|
case "System.Int32":
|
544
|
88
|
result = ((int)result1).CompareTo((int)result2);
|
545
|
88
|
break;
|
546
|
|
case "System.Int16":
|
547
|
0
|
result = ((int)result1).CompareTo((int)result2);
|
548
|
0
|
break;
|
549
|
|
case "System.Int64":
|
550
|
0
|
result = ((int)result1).CompareTo((int)result2);
|
551
|
0
|
break;
|
552
|
|
case "System.Char":
|
553
|
0
|
result = ((char)result1).CompareTo((char)result2);
|
554
|
0
|
break;
|
555
|
|
case "System.Double":
|
556
|
0
|
result = ((double)result1).CompareTo((double)result2);
|
557
|
0
|
break;
|
558
|
|
case "System.Decimal":
|
559
|
0
|
result = ((decimal)result1).CompareTo((decimal)result2);
|
560
|
0
|
break;
|
561
|
|
case "System.Boolean":
|
562
|
56
|
result = ((bool)result1).CompareTo((bool)result2);
|
563
|
56
|
break;
|
564
|
|
case "System.DateTime":
|
565
|
128
|
result = ((DateTime)result1).CompareTo((DateTime)result2);
|
566
|
128
|
break;
|
567
|
|
}
|
568
|
|
}
|
569
|
|
|
570
|
428
|
if (invertSort && (result != 0)) result = -result;
|
571
|
|
|
572
|
428
|
return result;
|
573
|
|
}
|
574
|
|
}
|
575
|
|
|
576
|
|
#endregion
|
577
|
|
}
|
578
|
|
|