forked from webgrid/WebGrid
-
Notifications
You must be signed in to change notification settings - Fork 0
/
GridMethods.cs
596 lines (520 loc) · 23.9 KB
/
GridMethods.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
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
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
/*
Copyright © Olav Christian Botterli.
Dual licensed under the MIT or GPL Version 2 licenses.
Date: 30.08.2011, Norway.
http://www.webgrid.com
*/
#region Header
/*
Copyright © Olav Christian Botterli.
Dual licensed under the MIT or GPL Version 2 licenses.
Date: 30.08.2011, Norway.
http://www.webgrid.com
*/
#endregion Header
namespace WebGrid
{
using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Text;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Collections;
using Config;
using Data.Database;
using Design;
using Enums;
using Util;
public partial class Grid
{
#region Fields
internal readonly List<string> ClientNotifications = new List<string>();
internal bool m_BindDataSource;
internal bool m_ActiveColumnFilter;
#endregion Fields
#region Properties
// internal object m_Datasourcedotnet;
// internal DataTable m_DataTableSettings;
// internal string m_XmlDataDocument;
// internal DataSet m_XmlDataSet;
// internal XmlNodeList m_XmlNodeList;
// internal string m_Xmlxpath;
/// <summary>
/// Gets or sets the source containing a list of values used to populate the items within the control.
/// </summary>
/// <value>The datasource.</value>
[Browsable(false)]
public object DataSource
{
get { return MasterTable.DataSource; }
set
{
MasterTable.DataSource = value;
}
}
#endregion Properties
#region Methods
/// <summary>
/// Finds the control.
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="startingControl">The starting control.</param>
/// <param name="id">The id.</param>
/// <returns></returns>
public static T FindControl<T>(Control startingControl, string id)
where T : Control
{
if (startingControl == null || string.IsNullOrEmpty(id))
return null;
T found = null;
foreach (Control activeControl in startingControl.Controls)
{
found = activeControl as T;
if (found == null || (string.Compare(id, activeControl.ID, true) != 0 && activeControl.HasControls()))
{
found = FindControl<T>(activeControl, id);
}
else if (string.Compare(id, found.ID, true) != 0)
found = null;
if (found != null)
{
break;
}
}
return found;
}
/// <summary>
/// Finds a WebGrid.Grid object within a Control.
/// </summary>
/// <param name="parent">The control to look into</param>
/// <param name="gridID">The grid to search for</param>
/// <returns></returns>
public static Grid FindGrid(Control parent, string gridID)
{
return FindControl<Grid>(parent, gridID);
}
///<summary>
/// The message to add.
///</summary>
///<param name="message"></param>
public void AddClientNotificationMessage(string message)
{
ClientNotifications.Add(message);
}
///<summary>
/// The message to add.
///</summary>
///<param name="message"></param>
///<param name="cookieName"></param>
///<param name="cookeTimeOut"></param>
public bool AddClientNotificationMessage(string message, string cookieName, System.DateTime cookeTimeOut)
{
if (HttpContext.Current.Request.Cookies[cookieName] != null || string.IsNullOrEmpty(cookieName))
return false;
ClientNotifications.Add(message);
HttpCookie cookie = new HttpCookie(cookieName);
cookie.Expires = cookeTimeOut;
HttpContext.Current.Response.Cookies.Add(cookie);
return true;
}
/// <summary>
/// Cancel your changes and return to grid view. (Applies to detail view)
/// This function is the same as "Cancel" button in detail view. It uses the same events and same methods.
/// </summary>
public void Cancel()
{
RecordCancelClickEvent((string.Format("RecordCancelClick!{0}", InternalId)).Split("!"[0]));
}
/// <summary>
/// Binds WebGrid and all its child controls to the data source.
/// </summary>
public override void DataBind()
{
base.OnDataBinding(EventArgs.Empty);
m_BindDataSource = true;
}
/// <summary>
/// Finds controls on the Page by ID string
/// </summary>
/// <typeparam name="T">The type to find</typeparam>
/// <param name="id">The identifer to find</param>
/// <returns></returns>
public T FindControl<T>(string id)
where T : Control
{
return FindControl<T>(Page, id);
}
/// <summary>
/// Save your changes to the data source for both updating and new records.
/// This function applies to both grid and detail view.
/// </summary>
public void Save()
{
if (Trace.IsTracing)
Trace.Trace("Start Save({0});", ID);
if (Page == null || MasterTable == null)
return;
m_GetColumnsPostBackData = true;
switch (DisplayView)
{
case DisplayView.Detail:
{
if (Trace.IsTracing)
Trace.Trace("Save(Edit)");
string status = string.Empty;
SetupGridRelations();
if (m_IsOneToOneRelationGrid)
status = InternalId == null ? "new" : "update";
RecordUpdateClickEvent(new[]
{
string.Format("RecordUpdateClick!{0}!{1}!{2}", InternalId, m_IsCopyClick,
status)
});
}
break;
case DisplayView.Grid:
if (Trace.IsTracing)
Trace.Trace("Save(Grid)");
UpdateRowsClickEvent();
break;
}
if (Trace.IsTracing)
Trace.Trace("Finish Save({0});", ID);
}
internal static void AddClientScript(WebGridHtmlWriter writer, string scriptcontent)
{
if (Anthem.Manager.IsCallBack)
Anthem.Manager.AddScriptForClientSideEval(scriptcontent);
else
writer.Write(string.Format(@"<script type=""text/javascript"">{0}</script>", scriptcontent));
}
internal static void FindEditorControls(ref GridCollection grids, ControlCollection controls)
{
for (int i = 0; i < controls.Count; i++)
{
if (controls[i] is Grid)
grids.Add((Grid)controls[i]);
if (controls[i].Controls.Count > 0)
FindEditorControls(ref grids, controls[i].Controls);
}
}
internal void BindDataSourceSession()
{
if (HttpContext.Current == null || HttpContext.Current.Session == null)
return;
MasterTable.ClearDataSourceSession();
if ( MasterTable.DataSource != null)
HttpContext.Current.Session[UniqueID + "_Datasourcedotnet"] = MasterTable.DataSource;
if (MasterTable.m_DataSourceColumns != null)
HttpContext.Current.Session[UniqueID + "_DataTableSettings"] = MasterTable.m_DataSourceColumns;
}
internal object GetState(string key)
{
return ViewState[key];
}
internal ICollection GetStateKeys()
{
return ViewState.Keys;
}
internal void ProcessSystemColumns()
{
bool haveDelete = false;
bool haveCopy = false;
bool haveSpacingTag = false;
for (int i = 0; i < MasterTable.Columns.Count; i++)
{
if (MasterTable.Columns[i].ColumnType != ColumnType.SystemColumn)
continue;
if (haveDelete == false &&
((SystemColumn)MasterTable.Columns[i]).SystemColumnType ==
Enums.SystemColumn.DeleteColumn)
haveDelete = true;
else if (haveCopy == false &&
((SystemColumn)MasterTable.Columns[i]).SystemColumnType ==
Enums.SystemColumn.CopyColumn)
haveCopy = true;
else if (haveSpacingTag == false &&
((SystemColumn)MasterTable.Columns[i]).SystemColumnType ==
Enums.SystemColumn.SpacingColumn)
{
haveSpacingTag = true;
MasterTable.Columns[i].WidthColumnHeaderTitle = Unit.Empty;
MasterTable.Columns[i].Title = string.Empty;
}
}
if (IsDesignTime) return;
if (haveCopy == false)
{
SystemColumn colcopy = new SystemColumn("wgSystemCopy", Enums.SystemColumn.CopyColumn, MasterTable)
{
DisplayIndex = 1,
Title = string.Empty,
Visibility = Visibility.Grid,
m_ColumnType = ColumnType.SystemColumn
};
MasterTable.Columns.Add(colcopy);
}
if (haveDelete == false)
{
SystemColumn coldelete = new SystemColumn("wgSystemDelete", Enums.SystemColumn.DeleteColumn, MasterTable)
{
DisplayIndex = (MasterTable.Columns.Count * 10) + 10,
Title = string.Empty,
Visibility = Visibility.Grid,
m_ColumnType = ColumnType.SystemColumn
};
MasterTable.Columns.Add(coldelete);
}
if (Width.Type == UnitType.Percentage || haveSpacingTag) return;
int prioritysystem = -1;
int prioritycolumn = -1;
MasterTable.Columns.ForEach(delegate(Column column)
{
if (column.ColumnType == ColumnType.SystemColumn &&
column.DisplayIndex > prioritysystem)
prioritysystem = column.DisplayIndex;
else if (column.DisplayIndex == -1 && column.DisplayIndex > prioritycolumn)
prioritycolumn = column.DisplayIndex;
});
SystemColumn col = new SystemColumn("wgspacingcolumn", Enums.SystemColumn.SpacingColumn, MasterTable)
{
DisplayIndex =
prioritycolumn > prioritysystem
? prioritycolumn + 1
: prioritysystem - 1,
Title = string.Empty,
Visibility = Visibility.Grid,
m_ColumnType = ColumnType.SystemColumn
};
MasterTable.Columns.Add(col);
}
internal void SetState(string key, object value)
{
ViewState[key] = value;
}
internal void State(string key, object value)
{
if (ViewState != null)
ViewState[key] = value;
}
// 24.03.2005 - jorn : added extra debug-info to foreignkeys.
internal void UpdateDebugString()
{
string debuginfo = null;
if (m_DebugString != null)
debuginfo = m_DebugString.ToString();
m_DebugString = new StringBuilder(string.Format("<br/><table width=\"{0}\" class=\"wgmaingrid\">", Width));
m_DebugString.Append("<tr><td colspan=\"9\" align=\"left\"><b>WebGrid debug information</b></td></tr>");
m_DebugString.Append("<tr class=\"wgrow\">");
m_DebugString.Append("<td><b>ColumnId:</b></td>");
m_DebugString.Append("<td><b>Default DataSourceId:</b></td>");
m_DebugString.Append("<td><b>DataSourceId:</b></td>");
m_DebugString.Append("<td><b>Primarykey:</b></td>");
m_DebugString.Append("<td><b>ColumnType:</b></td>");
m_DebugString.Append("<td><b>AllowEdit:</b></td>");
m_DebugString.Append("<td><b>IsInDataSource:</b></td>");
m_DebugString.Append("<td><b>DataSourceValue:</b></td>");
m_DebugString.Append("<td><b>Value:</b></td>");
m_DebugString.Append("<td><b>DisplayIndex:</b></td>");
m_DebugString.Append("</tr>");
for (int i = 0; i < MasterTable.Columns.Count; i++)
{
m_DebugString.Append("<tr class=\"wgrow\">");
m_DebugString.AppendFormat("<td>{0}</td>", MasterTable.Columns[i].ColumnId);
m_DebugString.AppendFormat("<td>{0}</td>", MasterTable.Columns[i].DefaultDataSourceId);
m_DebugString.AppendFormat("<td>{0}</td>", MasterTable.Columns[i].DataSourceId);
m_DebugString.AppendFormat("<td>{0}</td>", MasterTable.Columns[i].Primarykey);
m_DebugString.AppendFormat("<td>{0}</td>", MasterTable.Columns[i].ColumnType);
m_DebugString.AppendFormat("<td>{0}</td>", MasterTable.Columns[i].AllowEdit);
m_DebugString.AppendFormat("<td>{0}</td>", MasterTable.Columns[i].IsInDataSource);
if (DisplayView == DisplayView.Detail && MasterTable.Rows.Count > 0)
m_DebugString.AppendFormat("<td>{0}", MasterTable.Rows[0][i].DataSourceValue);
else
m_DebugString.AppendFormat("<td> N/A");
if (MasterTable.Columns[i].ColumnType == ColumnType.Foreignkey)
{
if (DisplayView == DisplayView.Grid || MasterTable.Rows.Count == 0)
m_DebugString.AppendFormat("<br/>ID:{0}", "N/A");
else
m_DebugString.AppendFormat("<br/>ID:{0}", MasterTable.Rows[0][i].Value);
m_DebugString.AppendFormat("<br/>ValueColumn: {0}",
((Foreignkey)MasterTable.Columns[i]).ValueColumn);
m_DebugString.AppendFormat("<br/>DataSourceID: {0}", MasterTable.Columns[i].DataSourceId);
}
m_DebugString.Append("</td>");
if (DisplayView == DisplayView.Detail && MasterTable.Rows.Count > 0)
m_DebugString.AppendFormat("<td>{0}", MasterTable.Rows[0][i].Value);
else
m_DebugString.AppendFormat("<td>{0}", "N/A");
try
{
if (MasterTable.Columns[i].ColumnType == ColumnType.Foreignkey && DisplayView == DisplayView.Detail && MasterTable.Rows.Count > 0)
m_DebugString.AppendFormat("<br/>ID:{0}", MasterTable.Rows[0][i].Value);
}
catch (Exception ee)
{
throw new GridException("Error creating debug information for column foreignkey column", ee);
}
m_DebugString.Append("</td>");
m_DebugString.AppendFormat("<td>{0}</td>", MasterTable.Columns[i].DisplayIndex);
m_DebugString.Append("</tr>");
}
m_DebugString.Append("<tr><td colspan=\"9\" align=\"left\"><b>Addition Debug information</b>");
m_DebugString.Append("</td></tr>");
m_DebugString.Append("<tr valign=\"top\"><td colspan=\"9\" valign=\"top\" align=\"left\">");
m_DebugString.AppendFormat("<b>{0}: Grid unique Id: {1}</b> <br/>", ID, base.ClientID);
m_DebugString.AppendFormat(
"<b>{0}: CacheGridStructure status is: {1}</b> (Used to cache system messages and data source structures)<br/>",
ID, CacheGridStructure);
m_DebugString.AppendFormat("<b>{0}: EnableCallBack (Ajax) status is: {1}</b> <br/>", ID, EnableCallBack);
m_DebugString.AppendFormat("{0}</td></tr>", debuginfo);
m_DebugString.Append("</table>");
}
private static string FixSelectedValue(string selectedValue)
{
if (selectedValue.Equals("between", StringComparison.OrdinalIgnoreCase))
return selectedValue;
if (selectedValue.Equals("true", StringComparison.OrdinalIgnoreCase))
selectedValue = "1";
else if (selectedValue.Equals("false", StringComparison.OrdinalIgnoreCase))
selectedValue = "0";
if (Equals(selectedValue.StartsWith("is", StringComparison.OrdinalIgnoreCase), true) ||
Equals(selectedValue.StartsWith("=", StringComparison.OrdinalIgnoreCase), true))
return selectedValue;
if (selectedValue.StartsWith(">", StringComparison.OrdinalIgnoreCase) ||
selectedValue.StartsWith("<", StringComparison.OrdinalIgnoreCase))
return selectedValue;
if (Equals(Validate.IsFloat(selectedValue), true))
return String.Format(" = {0}", selectedValue.Replace(",", "."));
if (Equals(selectedValue.StartsWith("%", StringComparison.OrdinalIgnoreCase), true))
return String.Format(" LIKE '{0}'", selectedValue);
if (selectedValue.StartsWith("'%", StringComparison.OrdinalIgnoreCase))
return String.Format(" LIKE {0}", selectedValue);
if (selectedValue.StartsWith("'", StringComparison.OrdinalIgnoreCase) == false &&
selectedValue.EndsWith("'", StringComparison.OrdinalIgnoreCase) == false)
return String.Format(" = '{0}'", selectedValue);
return selectedValue;
}
/// <summary>
/// Deeps the search master of WebGrid.
/// </summary>
/// <param name="grid">The grid.</param>
/// <returns></returns>
private static bool RecursiveSearchMasterWebGrid(Grid grid)
{
if (grid.MasterWebGrid.DisplayView == DisplayView.Detail && Equals(grid.RequiredbyMasterWebGrid(), true))
return grid.MasterWebGrid.MasterWebGrid == null || RecursiveSearchMasterWebGrid(grid.MasterWebGrid);
return false;
}
private string BuildFilterByColumnSql()
{
if (!GotHttpContext)
return string.Empty;
StringBuilder filter = new StringBuilder(null);
int i = 0;
while (i < MasterTable.Columns.Count)
{
Column column = MasterTable.Columns[i];
i++;
string selectedValue = null;
if (column.ColumnType == ColumnType.DateTime)
selectedValue = ((DateTime)column).SearchFilter;
else if (GotHttpContext &&
HttpContext.Current.Request.Form[string.Format("ddl{0}", column.ClientHeaderId)] != null)
{
selectedValue =
HttpUtility.HtmlEncode(
HttpContext.Current.Request.Form[string.Format("ddl{0}", column.ClientHeaderId)]);
}
else if (GetState(string.Format("ddl{0}", column.ClientHeaderId)) != null)
selectedValue = GetState(string.Format("ddl{0}", column.ClientHeaderId)) as string;
if (string.IsNullOrEmpty(selectedValue))
continue;
if (filter.Length > 0)
filter.Append(" AND ");
filter.AppendFormat("{0} {1}",
Interface.BuildTableElement(true, MasterTable.DataSourceId,
column.ColumnId),
FixSelectedValue(selectedValue));
}
m_ActiveColumnFilter = filter.Length > 0;
return filter.ToString();
}
private void DisableShowRequiredIconIfThereIsNoRequiredFields()
{
if (DisplayView == DisplayView.Grid || DisplayRequiredColumn == false || DesignMode)
return;
if (MasterTable.Rows.Count <= 0 || !DisplayRequiredColumn) return;
bool status = false;
for (int i = 0; i < MasterTable.Columns.Count; i++)
{
Column column = MasterTable.Columns[i];
if (!column.Required || !Equals(column.AllowEdit, true) ||
(column.Visibility != Visibility.Both && column.Visibility != Visibility.Detail)) continue;
status = true;
break;
}
DisplayRequiredColumn = status;
}
private bool RequiredbyMasterWebGrid()
{
return ((!m_IsGridInsideGrid || MasterWebGrid.InternalId != null) && (MasterWebGrid.Visible &&
((((!MasterWebGrid.DisplaySlaveGridMenu || MasterWebGrid.ActiveMenuSlaveGrid == this)
&& MasterWebGrid.InternalId != null) && MasterWebGrid.DisplayView == DisplayView.Detail) ||
m_IsGridInsideGrid))) &&
(MasterWebGrid.MasterWebGrid == null || RecursiveSearchMasterWebGrid(MasterWebGrid) || m_IsGridInsideGrid);
}
/// <summary>
/// </summary>
/// <exclude/>
/// <returns></returns>
[EditorBrowsable(EditorBrowsableState.Never)]
private bool ShouldSerializeConnectionString()
{
if (string.IsNullOrEmpty(ConnectionString) == false &&
(string.IsNullOrEmpty(GridConfig.Get("WGConnectionString", null as string))
|| ConnectionString.Equals(GridConfig.Get("WGConnectionString", null as string)) == false))
{
return true;
}
return Equals(m_ShouldSerializeConnectionString, true) ? true : false;
}
/// <summary>
/// </summary>
/// <exclude/>
/// <returns></returns>
[EditorBrowsable(EditorBrowsableState.Never)]
private bool ShouldSerializeDataSource()
{
return false;
}
/// <summary>
/// </summary>
/// <exclude/>
/// <returns></returns>
[EditorBrowsable(EditorBrowsableState.Never)]
private bool ShouldSerializeDataSourceId()
{
return !string.IsNullOrEmpty(MasterTable.DataSourceId) &&
MasterTable.DataSourceId.Equals(DATASOURCEID_NULL) == false;
}
/// <summary>
/// </summary>
/// <exclude/>
/// <returns></returns>
[EditorBrowsable(EditorBrowsableState.Never)]
private bool ShouldSerializeDatabaseConnectionType()
{
return false;
}
private void UpdateDebug()
{
if (!Debug) return;
UpdateDebugString();
}
#endregion Methods
}
}