下面的代码是写一个登录控件的VB.NET版. Imports System Imports System.Web Imports System.Web.UI Imports System.Web.UI.WebControls
Namespace myControls
Public Class Login Inherits Control Implements INamingContainer
Public Property Username As String Get Me.EnsureChildControls() Return CType( Controls( 2 ), TextBox ).Text End Get Set Me.EnsureChildControls() CType( Controls( 2 ), TextBox ).Text = Value End Set End Property
Public Property Password As String Get Me.EnsureChildControls() Return CType( Controls( 5 ), TextBox ).Text End Get Set Me.EnsureChildControls() CType( Controls( 5 ), TextBox ).Text = Value End Set End Property
Protected Overrides Sub CreateChildControls() Me.Controls.Add( New LiteralControl( "<div style=""border: 5px " & _ "inset #cccccc;background-color:#eeeeee;width:50%;padding:10px"">" ) )
' Add Username Me.Controls.Add( New LiteralControl( "<b>Username:</b> " ) ) Me.Controls.Add( New TextBox ) Me.Controls.Add( New LiteralControl( "<p>" ) )
' Add Password Dim txtPass As New TextBox Me.Controls.Add( New LiteralControl( "<b>Password:</b> " ) ) txtPass.TextMode = TextBoxMode.Password Me.Controls.Add( txtPass ) Me.Controls.Add( New LiteralControl( "<p>" ) )
' Add Submit Button Dim btnButton As New Button btnButton.Text = "Login!" Me.Controls.Add( btnButton ) Me.Controls.Add( New LiteralControl( "</div>" ) ) End Sub
End Class
End Namespace 其中的属性 Public Property Username As String Get Me.EnsureChildControls() Return CType( Controls( 2 ), TextBox ).Text End Get Set Me.EnsureChildControls() CType( Controls( 2 ), TextBox ).Text = Value End Set End Property 如果用C#写该如何写?大家多指教.
[此贴子已经被作者于2005-9-7 11:44:28编辑过]