标题:组合控件的问题.高手请进
只看楼主
xc_lw
Rank: 1
等 级:新手上路
帖 子:40
专家分:0
注 册:2004-11-12
 问题点数:0 回复次数:1 
组合控件的问题.高手请进

下面的代码是写一个登录控件的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编辑过]

搜索更多相关主题的帖子: 控件 
2005-09-07 11:43
xc_lw
Rank: 1
等 级:新手上路
帖 子:40
专家分:0
注 册:2004-11-12
得分:0 
我已经知道了.写成
public string Username

{
    get
     {
         this.EnsureChildControls();
         return ((TextBox)Controls[2]).Text;
      }
    set
      {
         this.EnsureChildControls();
         ((TextBox)Controls[2]).Text=value;
       }      
}

2005-09-08 13:58



参与讨论请移步原网站贴子:https://bbs.bccn.net/thread-26937-1-1.html




关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.030115 second(s), 8 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved