Programming Microsoft ASP.NET 4 - Dino Esposito [104]
WebResource.axd?a=assembly&r=resourceName&t=timestamp
The timestamp value is the current timestamp of the assembly, and it is added to make the browser download resources again if the assembly is modified.
Controls-Related Methods
Table 5-12 details a bunch of helper methods on the Page class architected to let you manage and validate child controls and resolve URLs.
Table 5-12. Helper Methods of the Page Object
Method
Description
DesignerInitialize
Initializes the instance of the Page class at design time, when the page is being hosted by RAD designers such as Visual Studio.
FindControl
Takes a control’s ID and searches for it in the page’s naming container. The search doesn’t dig out child controls that are naming containers themselves.
GetTypeHashCode
Retrieves the hash code generated by ASP.xxx_aspx page objects at run time. In the base Page class, the method implementation simply returns 0; significant numbers are returned by classes used for actual pages.
GetValidators
Returns a collection of control validators for a specified validation group.
HasControls
Determines whether the page contains any child controls.
LoadControl
Compiles and loads a user control from an .ascx file, and returns a Control object. If the user control supports caching, the object returned is PartialCachingControl.
LoadTemplate
Compiles and loads a user control from an .ascx file, and returns it wrapped in an instance of an internal class that implements the ITemplate interface. The internal class is named SimpleTemplate.
MapPath
Retrieves the physical, fully qualified path that an absolute or relative virtual path maps to.
ParseControl
Parses a well-formed input string, and returns an instance of the control that corresponds to the specified markup text. If the string contains more controls, only the first is taken into account. The runat attribute can be omitted. The method returns an object of type Control and must be cast to a more specific type.
RegisterRequiresControlState
Registers a control as one that requires control state.
RegisterRequiresPostBack
Registers the specified control to receive a postback handling notice, even if its ID doesn’t match any ID in the collection of posted data. The control must implement the IPostBackDataHandler interface.
RegisterRequiresRaiseEvent
Registers the specified control to handle an incoming postback event. The control must implement the IPostBackEventHandler interface.
RegisterViewStateHandler
Mostly for internal use, the method sets an internal flag that causes the page view state to be persisted. If this method is not called in the prerendering phase, no view state will ever be written. Typically, only the HtmlForm server control for the page calls this method. There’s no need to call it from within user applications.
ResolveUrl
Resolves a relative URL into an absolute URL based on the value of the TemplateSourceDirectory property.
Validate
Instructs any validation controls included in the page to validate their assigned information. If defined in the page, the method honors ASP.NET validation groups.
The methods LoadControl and LoadTemplate share a common code infrastructure but return different objects, as the following pseudocode shows:
public Control LoadControl(string virtualPath)
{
Control ascx = GetCompiledUserControlType(virtualPath);
ascx.InitializeAsUserControl();
return ascx;
}
public ITemplate LoadTemplate(string virtualPath)
{
Control ascx = GetCompiledUserControlType(virtualPath);
return new SimpleTemplate(ascx);
}
Both methods differ from the ParseControl method in that the latter never causes compilation but simply parses the string and infers control information. The information is then used to create and initialize a new instance of the control class. As mentioned, the runat attribute is unnecessary in this context. In ASP.NET, the runat attribute is key, but in practice, it has no other role than marking the