forked from extnet/Ext.NET
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBaseControl.cs
More file actions
316 lines (274 loc) · 9.09 KB
/
BaseControl.cs
File metadata and controls
316 lines (274 loc) · 9.09 KB
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
/********
* @version : 2.1.1 - Ext.NET Pro License
* @author : Ext.NET, Inc. http://www.ext.net/
* @date : 2012-12-10
* @copyright : Copyright (c) 2007-2012, Ext.NET, Inc. (http://www.ext.net/). All rights reserved.
* @license : See license.txt and http://www.ext.net/license/.
********/
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Globalization;
using System.Linq;
using System.Web.Mvc;
using Ext.Net.Utilities;
namespace Ext.Net
{
/// <summary>
///
/// </summary>
public partial class BaseControl
{
/// <summary>
///
/// </summary>
[Browsable(false)]
[DefaultValue(false)]
[DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
[Description("")]
public virtual bool IsMVC
{
get
{
return Ext.Net.MVC.MvcResourceManager.IsMVC || this.Page is System.Web.Mvc.ViewPage;
}
}
private System.Web.Mvc.ViewContext viewContext;
public System.Web.Mvc.ViewContext ViewContext
{
get
{
if (this.viewContext == null)
{
if (this.Page is System.Web.Mvc.ViewPage)
{
this.viewContext = ((System.Web.Mvc.ViewPage)this.Page).ViewContext;
}
else
{
BaseControl control = this.ParentWebControl;
if (control != null)
{
this.viewContext = control.ViewContext;
}
}
}
return this.viewContext;
}
set
{
this.viewContext = value;
}
}
private System.Web.Mvc.ControllerBase controller;
public System.Web.Mvc.ControllerBase Controller
{
get
{
if (this.controller == null)
{
if (this.Page is System.Web.Mvc.ViewPage)
{
this.controller = ((System.Web.Mvc.ViewPage)this.Page).ViewContext.Controller;
}
BaseControl control = this.ParentWebControl;
if (control != null)
{
this.controller = control.Controller;
}
}
return this.controller;
}
set
{
this.controller = value;
}
}
public virtual ViewDataDictionary ViewData
{
get
{
if (this.Page is System.Web.Mvc.ViewPage)
{
return ((System.Web.Mvc.ViewPage)this.Page).ViewData;
}
else
{
if (this.ViewContext != null)
{
return this.ViewContext.ViewData;
}
if (this.Controller != null)
{
return this.Controller.ViewData;
}
return null;
}
}
}
public virtual ControllerContext ControllerContext
{
get
{
if (this.Page is System.Web.Mvc.ViewPage)
{
return ((System.Web.Mvc.ViewPage)this.Page).ViewContext.Controller.ControllerContext;
}
else
{
if (this.ViewContext != null)
{
return this.ViewContext.Controller.ControllerContext;
}
if (this.Controller != null)
{
return this.Controller.ControllerContext;
}
return null;
}
}
}
/// <summary>
///
/// </summary>
[Meta]
[DefaultValue(null)]
[Description("")]
public virtual string ControlFor
{
get
{
return this.State.Get<string>("ControlFor", null);
}
set
{
this.State.Set("ControlFor", value);
if (this.onMvcInit)
{
this.SetControlFor(null);
}
}
}
/// <summary>
///
/// </summary>
[Meta]
[DefaultValue(false)]
[Description("")]
public virtual bool IDFromControlFor
{
get
{
return this.State.Get<bool>("IDFromControlFor", false);
}
set
{
this.State.Set("IDFromControlFor", value);
}
}
private bool onMvcInit = false;
private bool setControlForOnInit = true;
partial void OnMvcInit()
{
if (this.ControlFor != null && this.setControlForOnInit)
{
this.SetControlFor(null);
}
this.onMvcInit = true;
}
[Meta]
[DefaultValue(null)]
public string FormatControlForValue
{
get;
set;
}
[Meta]
[DefaultValue(null)]
public Func<object, object> ConvertControlForValue
{
get;
set;
}
/// <summary>
///
/// </summary>
protected internal virtual void SetControlFor(ModelMetadata meta)
{
string controlFor = this.ControlFor;
if (controlFor == null)
{
return;
}
ViewDataDictionary viewData = this.ViewData;
ControllerContext controllerContext = this.ControllerContext;
if (viewData == null || controllerContext == null)
{
return;
}
this.SuspendScripting();
this.setControlForOnInit = false;
string fullName = viewData.TemplateInfo.GetFullHtmlFieldName(controlFor);
ModelMetadata metadata = meta ?? ModelMetadata.FromStringExpression(controlFor, viewData);
this.OnMetadataProcess(metadata, fullName, viewData, controllerContext);
object value = metadata.Model;
if (this.ConvertControlForValue != null)
{
value = this.ConvertControlForValue(value);
}
this.SetModelValue(this.FormatControlForValue.IsEmpty() ? value : this.FormatValue(value, this.FormatControlForValue));
if (this.IDFromControlFor)
{
this.ID = TagBuilder.CreateSanitizedId(fullName, System.Web.WebPages.Html.HtmlHelper.IdAttributeDotReplacement);
}
foreach (KeyValuePair<string, object> adValue in metadata.AdditionalValues)
{
this.SetModelAdditionalValue(adValue);
}
foreach (ModelValidator v in metadata.GetValidators(controllerContext))
{
ModelClientValidationRule rule = v.GetClientValidationRules().FirstOrDefault();
if (rule != null)
{
this.SetModelValidationRule(rule);
}
}
this.ResumeScripting();
}
protected virtual string FormatValue(object value, string format)
{
if (value == null)
{
return String.Empty;
}
if (String.IsNullOrEmpty(format))
{
return Convert.ToString(value, CultureInfo.CurrentCulture);
}
else
{
return String.Format(CultureInfo.CurrentCulture, format, value);
}
}
protected virtual void OnMetadataProcess(ModelMetadata meta, string name, ViewDataDictionary viewData, ControllerContext context)
{
if (meta.EditFormatString.IsNotEmpty())
{
this.FormatControlForValue = meta.EditFormatString;
}
else if (meta.DisplayFormatString.IsNotEmpty())
{
this.FormatControlForValue = meta.DisplayFormatString;
}
}
protected virtual void SetModelAdditionalValue(KeyValuePair<string, object> value)
{
}
protected virtual void SetModelValidationRule(ModelClientValidationRule rule)
{
}
protected virtual void SetModelValue(object value)
{
}
}
}