forked from statisticssweden/PxWeb
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMarkerControlBase.vb
More file actions
231 lines (190 loc) · 8.52 KB
/
MarkerControlBase.vb
File metadata and controls
231 lines (190 loc) · 8.52 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
Imports System.Web.UI.WebControls
Imports System.Web.UI
Imports PCAxis.Web.Core.Exceptions
Imports System.Web.Caching
Imports System.Web.UI.Design
Imports System.ComponentModel.Design
Imports System.Globalization
Imports System.IO
Imports PCAxis.Web.Core.Interfaces
''' <summary>
''' Base class for all marker controls. Marker controls are used to store public properties and
''' methods and enables user controls to be visible in a toolbox by representing them
''' </summary>
''' <typeparam name="TControl">The type of the usercontrol</typeparam>
''' <typeparam name="TMarker">The type of the markercontrol</typeparam>
''' <remarks></remarks>
Public MustInherit Class MarkerControlBase(Of TControl As ControlBase(Of TControl, TMarker), TMarker As MarkerControlBase(Of TControl, TMarker))
Inherits CompositeControl
Implements IMarkerControl
Private Const CACHE_KEY_VIRTUALPATHPROVIDERREGISTRED As String = "PCAxis.Web.Core.IsVirtualPathProviderRegistred"
Private Shared Logger As log4net.ILog = log4net.LogManager.GetLogger(GetType(MarkerControlBase(Of TControl, TMarker)))
'Constants
Private Const DESIGN_FILE_FORMAT As String = "{0}.ascx"
'Private variables
Private _isControlLoaded As Boolean
''' <summary>
''' Gets or sets whether the control is completely loaded
''' </summary>
''' <value>If <c>True</c> then the control is completely loaded, otherwise it's not</value>
''' <returns><c>True</c> if the control is completely loaded, otherwise <c>False</c></returns>
''' <remarks></remarks>
Protected Friend ReadOnly Property IsControlLoaded() As Boolean
Get
Return Me._isControlLoaded
End Get
End Property
'Private Properties
''' <summary>
''' Gets whether the controls state is being loaded or not
''' </summary>
''' <value>If <c>True</c> then state is being loaded, otherwise state is not being loaded</value>
''' <returns><c>True</c> is state is being loaded, otherwise <c>False</c></returns>
''' <remarks>Inherited controls should call this in properties that perform tasks when set</remarks>
Protected ReadOnly Property IsLoadingState() As Boolean
Get
If IsControlLoaded Then
Return Me.Control.IsLoadingState
End If
Return True
End Get
End Property
Private _control As TControl
''' <summary>
''' Gets a reference to the usercontrol that the markercontrol represents
''' </summary>
''' <value>A strongly typed reference to the usercontrol that the markercontrol represents</value>
''' <returns>The instance of the usercontrol that the markercontrol represents</returns>
''' <remarks></remarks>
Protected Property Control() As TControl
Get
Return _control
End Get
Private Set(ByVal value As TControl)
_control = value
End Set
End Property
''' <summary>
''' Gets a embedded resource the executing assembly
''' </summary>
''' <param name="resourceName">Name of the embedded resource</param>
''' <returns>A string representing the content</returns>
''' <remarks></remarks>
Private Function GetResource(ByVal resourceName As String) As String
Using reader As New StreamReader(Me.GetType().Assembly.GetManifestResourceStream _
(Me.GetType(), resourceName))
Return reader.ReadToEnd()
End Using
End Function
''' <summary>
''' Loads the embedded ascx file and displays it
''' </summary>
''' <remarks></remarks>
Private Sub LoadAscxFile()
If Not _isControlLoaded Then
Dim ascxControl As ControlBase(Of TControl, TMarker) = Nothing
'If in visual studio
If Me.DesignMode Or Me.Page Is Nothing Then
'Load the content of the ascx file and display it in a LiteralControl
Me.Controls.Add(New LiteralControl(GetResource(String.Format(CultureInfo.InvariantCulture, _
DESIGN_FILE_FORMAT, Me.GetType().Name))))
Return
Else
'Checks to see if we already have registred a AssemblyResourceProvider, otherwise register it
If Me.Page.Cache.Get(CACHE_KEY_VIRTUALPATHPROVIDERREGISTRED) Is Nothing Then
System.Web.Hosting.HostingEnvironment.RegisterVirtualPathProvider(New AssemblyResourceProvider())
Me.Page.Cache.Add(CACHE_KEY_VIRTUALPATHPROVIDERREGISTRED, _
"True", Nothing, Cache.NoAbsoluteExpiration, _
Cache.NoSlidingExpiration, CacheItemPriority.NotRemovable, Nothing)
End If
Try
'Load the ascxcontrol that this marker control represents
'using this class assembly and name
ascxControl = CType(Me.Page.LoadControl(String.Format( _
"~/PCAxis_Web_Controls/{0}.dll/{1}.ascx", _
Me.GetType().Assembly.GetName.Name, _
Me.GetType.FullName)), ControlBase(Of TControl, TMarker))
'Make the ascx control have the same AppRelativeTemplateSourceDirectory as the markercontrol
ascxControl.AppRelativeTemplateSourceDirectory = Me.AppRelativeTemplateSourceDirectory
Catch ex As Exception
Logger.Error("Error loading ascx for " + Me.GetType.Name, ex)
Throw ex
End Try
End If
'If there is no control loaded, throw an exception
If (ascxControl Is Nothing) Then
Throw _
New AscxLoadException("Could not load control from ascx")
End If
'Set the Control reference to the loaded ascx control
Me.Control = CType(ascxControl, TControl)
'Set the ascx controls Marker reference to this markercontrol
ascxControl.Marker = CType(Me, TMarker)
'make sure both controls have the same id
Me.Control.ID = Me.ID
'Add control to control collection to enable rendering
Me.Controls.Add(ascxControl)
_isControlLoaded = True
End If
End Sub
''' <summary>
''' Ensures an id is created for the marker
''' </summary>
''' <remarks></remarks>
Public Sub New()
Me.EnsureID()
End Sub
''' <summary>
''' Called by the ASP.NET page framework to notify server controls that use composition-based implementation to create any
''' child controls they contain in preparation for posting back or rendering.
''' </summary>
''' <remarks></remarks>
Protected Overrides Sub CreateChildControls()
LoadAscxFile()
MyBase.CreateChildControls()
End Sub
''' <summary>
''' Recreates the child controls in the markercontrol
''' </summary>
''' <remarks></remarks>
Protected Overrides Sub RecreateChildControls()
Me.EnsureChildControls()
End Sub
''' <summary>
''' Raises the Init event
''' </summary>
''' <param name="e">An EventArgs object that contains the event data. </param>
''' <remarks>Used to load the ascx control</remarks>
Protected Overrides Sub OnInit(ByVal e As EventArgs)
Me.EnsureChildControls()
MyBase.OnInit(e)
End Sub
''' <summary>
''' Gets the <see cref="HtmlTextWriterTag" /> value that corresponds to this Web server control
''' </summary>
''' <value>The <see cref="HtmlTextWriterTag" /> enumeration value to use to render this control</value>
''' <returns>One of the <see cref="HtmlTextWriterTag" /> enumeration values</returns>
''' <remarks></remarks>
Protected Overrides ReadOnly Property TagKey() As HtmlTextWriterTag
Get
Return HtmlTextWriterTag.Div
End Get
End Property
''' <summary>
''' Gets or sets the programmatic identifier assigned to the server control.
''' </summary>
''' <value>A programmatic identifier assigned to the control to differate it from the other controls</value>
''' <returns>The programmatic identifier assigned to the control.</returns>
''' <remarks>Overridden to set the underlying ascx control to the same id</remarks>
Public Overrides Property ID() As String
Get
Return MyBase.ID
End Get
Set(ByVal value As String)
MyBase.ID = value
If Control IsNot Nothing Then
Control.ID = value
End If
End Set
End Property
End Class