Question 3) TS: Microsoft .NET Framework 2.0

Posted on
Like what you see? Share it.Share on Google+Share on LinkedInShare on FacebookShare on RedditTweet about this on TwitterEmail this to someone

Objective: Creating Custom Web Controls
SubObjective: Develop a templated control

Single Answer Multiple Choice

 

You are a Web application developer for a company. You decide to create a Web server control that can act as a template for creating Web server controls by other developers.

 

Which interface will your Web server control class implement to create a templated Web server control?

 

A. INamingContainer
B. ITemplate
C. IControlBuilderAccesssor
D. INavigateUIData

 

Answer:
A. INamingContainer

 

Tutorial:
You should implement the INamingContainer interface in your Web server control. This interface ensures that all child Web server controls defined in the templated Web server control contain unique values in the ID attribute. A templated Web server control allows developers to create Web server controls in the defined format. There are at least two control classes that must be implemented to create a templated Web server control: a “child” control class that represents the data to display and the “placeholder” control class that will place the child control into the template. The following code demonstrates how to create a templated Web server control for displaying customer information:

 

‘Child control class

 

Public Class CustomerItem
Inherits Control
Implements INamingContainer

 

Private fname, lname As String
‘Properties
Public Property FirstName() As String
Get
Return fname
End Get
Set (ByVal value As String)
Fname = Value
End Set
End Property
Public Property LastName() As String
Get
Return lname
End Get
Set (ByVal value As String)
lname = Value
End Set
End Property
End Class

 

‘Templated Control Class
<ParseChildren(True),
ToolboxData( “<{0}:Customer runat=server></{0}:Customer>”)> _
Public Class Customer
Inherits Control
Implements INamingContainer

 

‘Needed to hold the template value
Private _template As ITemplate
Private currentCustomer As New CustomerItem

 

‘Template property
<TemplateContainer(GetType(CustomerItem))> _
Public Property CustomerTemplate As ITemplate
Get
Return template
End Get
Set
template = Value
End Set
End Property

 

‘Properties exposed of the CustomerItem class
Public Property FirstName() As String
Get
Return currentCustomer.FirstName
End Get
Set (ByVal value As String)
currentCustomer.FirstName = Value
End Set
End Property

 

Public Property LastName() As String
Get
Return currentCustomer.LastName
End Get
Set (ByVal value As String)
currentCustomer.LastName = Value
End Set
End Property

 

Public Overrides Sub DataBind()
EnsureChildControls()
MyBase.DataBind()
End Sub

 

‘Must override to take the template, place child control inside
‘and add to the parent

 

Protected Overrides Sub CreateChildControls()
If Not (CustomerTemplate Is Nothing)
Controls.Clear()
CustomerTemplate.InstantiateIn(currentCustomer)
Controls.Add(currentCustomer)
End If
End Sub
End Class

 

You should not implement the ITemplate directly because this type is used to define the property you are required to create in a templated Web server control (see code above). If ITemplate is used in this context, run-time errors will be generated.

 

You should not implement the IControlBuilderAccesssor interface because this interface is used to ensure that the control’s builder is accessible to the control’s serializer. In this scenario, you are required to create a templated Web server control.

 

You should not implement the INavigateUIData interface because this interface is used to provide classes with the data for navigation user interface and navigation control values. In this scenario, you are required to create a templated Web server control.

 

Reference:
ASP.NET QuickStart Tutorial, “ASP.NET Web Forms,” “Authoring Custom Controls,” “Developing a Templated Control.”

 

MSDN Library, “.NET Development,” “.NET Framework SDK,” “Class Library Reference,” “System.Web.UI,” “INamingContainer Interface.”

 

These questions are derived from the Self Test Software Practice Test for Microsoft exam #70-528 – TS: Microsoft .NET Framework 2.0 – Web-Based Client Development (VB.NET)

Like what you see? Share it.Share on Google+Share on LinkedInShare on FacebookShare on RedditTweet about this on TwitterEmail this to someone
cmadmin

ABOUT THE AUTHOR

Posted in Archive|

Comment: