Beispiel für eine benutzerdefinierte Eigenschaft
Im folgenden Beispiel werden alle unter Eigenschaftentypen erläuterten Elemente miteinander kombiniert. Dabei werden eine String-Eigenschaft, eine Integer-Eigenschaft, eine Enumerationseigenschaft und eine Klasseneigenschaft definiert. Außerdem wird gezeigt, wie diese Eigenschaften gesetzt werden, wenn das Steuerelement deklarativ auf einer ASP.NET-Seite verwendet wird. Eine Anleitung zum Erstellen des Beispiels finden Sie unter Beispiele für Serversteuerelemente.
using System;
using System.ComponentModel;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
namespace CustomControls
{
// The Position enumeration.
public enum Position
{
Forward = 0,
Mid = 1,
Defence = 2,
Goalee = 3,
}
// The Address class.
public class Address
{
private String street = null;
private String city = null;
private String state = null;
private String zip = null;
// The Street property.
public String Street
{
get
{
return street;
}
set
{
street = value;
}
}
// The City property.
public String City
{
get
{
return city;
}
set
{
city = value;
}
}
// The State property.
public String State
{
get
{
return state;
}
set
{
state = value;
}
}
// The Zip property.
public String Zip
{
get
{
return zip;
}
set
{
zip = value;
}
}
}
// SoccerPlayer is control that exposes simple, enumeration, and
// class properties.
public class SoccerPlayer : Control
{
private String name = null;
private Position position;
private Address address = new Address();
private int age = 0;
// Age is a simple property of type integer.
// The element in square brackets is an attribute
// that provides additional information at design time.
[Description("The age of the player")]
public int Age
{
get
{
return age;
}
set
{
age = value;
}
}
// Name is a simple property of type String.
[Description("The name of the player")]
public String Name
{
get
{
return name;
}
set
{
name = value;
}
}
// PlayerPosition is an enumeration property whose
// type is the Position enumeration.
[Description("The position of the player on the field.")]
public Position PlayerPosition
{
get
{
return position;
}
set
{
position = value;
}
}
// PlayerAddress is a complex property whose
// type is the class Address. Address has four
// properties, Street, City, State, and Zip. These become
// subproperties of SoccerPlayer.
public Address PlayerAddress
{
get
{
return address;
}
}
//The Render method is inherited from Control.
//
protected override void Render(HtmlTextWriter output)
{
output.Write("Name: " + Name + "<br>");
output.Write("Age: " + Age + "<br>");
output.Write("PlayerPosition: " + PlayerPosition + "<br>");
output.Write("Address: " + PlayerAddress.Street + ", " + PlayerAddress.City + ", " +
PlayerAddress.State + ", " + PlayerAddress.Zip + "<br>");
}
}
}
[Visual Basic]
Option Strict
Option Explicit
Imports System
Imports System.ComponentModel
Imports System.Web
Imports System.Web.UI
Imports System.Web.UI.WebControls
Namespace CustomControls
' The Position enumeration.
Public Enum Position
Forward = 0
Mid = 1
Defence = 2
Goalee = 3
End Enum
' The Address class.
Public Class Address
Private _street As String = Nothing
Private _city As String = Nothing
Private _state As String = Nothing
Private _zip As String = Nothing
' The Street property.
Public Property Street() As String
Get
Return _street
End Get
Set
_street = value
End Set
End Property
' The City property.
Public Property City() As String
Get
Return _city
End Get
Set
_city = value
End Set
End Property
' The State property.
Public Property State() As String
Get
Return _state
End Get
Set
_state = value
End Set
End Property
' The Zip property.
Public Property Zip() As String
Get
Return _zip
End Get
Set
_zip = value
End Set
End Property
End Class
' SoccerPlayer is control that exposes simple, enumeration, and
' class properties.
Public Class SoccerPlayer
Inherits Control
Private _name As String = Nothing
Private _position As Position
Private _address As New Address()
Private _age As Integer = 0
' Age is a simple property of type integer.
' The element in square brackets is an attribute
' that provides additional information at design time.
<Description("The age of the player")> _
Public Property Age() As Integer
Get
Return _age
End Get
Set
_age = value
End Set
End Property
' Name is a simple property of type String.
<Description("The name of the player")> _
Public Property Name() As String
Get
Return _name
End Get
Set
_name = value
End Set
End Property
' PlayerPosition is an enumeration property whose
' type is the Position enumeration.
<Description("The position of the player on the field.")> _
Public Property PlayerPosition() As Position
Get
Return _position
End Get
Set
_position = value
End Set
End Property
' PlayerAddress is a complex property whose
' type is the class Address. Address has four
' properties, Street, City, State, and Zip. These become
' subproperties of SoccerPlayer.
Public ReadOnly Property PlayerAddress() As Address
Get
Return _address
End Get
End Property
'The Render method is inherited from Control.
'
Protected Overrides Sub Render(output As HtmlTextWriter)
output.Write(("Name: " & Name & "<br>"))
output.Write(("Age: " & Age & "<br>"))
output.Write(("PlayerPosition: " & PlayerPosition & "<br>"))
output.Write(("Address: " & PlayerAddress.Street & ", " & _
PlayerAddress.City & ", " & PlayerAddress.State & ", " & _
PlayerAddress.Zip & "<br>"))
End Sub
End Class
End Namespace
Deklaratives Festlegen der Eigenschaften eines Steuerelements
Auf der folgenden ASP.NET-Seite wird das SoccerPlayer
-Steuerelement verwendet, und die Eigenschaften werden unter Verwendung deklarativer Syntax festgelegt.
<%@ Register TagPrefix="Custom" Namespace="CustomControls" Assembly = "CustomControls" %>
<html>
<body>
The SoccerPlayer control has four properties of the following types:
string, integer, enumeration, and class.
<br>
<b>Name</b> is a simple property of type <b>String</b>.<br>
<b>Age</b> is a primitive property of type <b>integer</b>.<br>
<b>PlayerPosition</b> is a simple property of type <b>enumeration</b>.<br>
<b>PlayerAddress</b> is a complex property of type <b>Address</b>,
which is a <b>class</b> that itself has four
properties: <br> Street, City, State, and Zip.
These properties are called subproperties of SoccerPlayer.
<br>
Notice the syntax for setting the enumeration property and the subproperties.
<br><br>
<b>
<Custom:SoccerPlayer id = "Properties" Name = "Your Favorite Player's Name" Age = "20" PlayerPosition = "Forward"
PlayerAddress-Street = "100, Players Lane" PlayerAddress-City = "Soccer City"
PlayerAddress-State = "State" PlayerAddress-Zip = "000000" runat=server/>
</b>
</body>
</html>