定义用户控件属性,用户控件值的传递

新建一个用户控件,并创建控件公共属性。WebUserControl.ascx
<%@ Control Language="vb" AutoEventWireup="false" Codebehind="WebUserControl.ascx.vb" Inherits="test.WebUserControl" TargetSchema="http://schemas.microsoft.com/intellisense/ie5" %>
<asp:DropDownList id="D1" runat="server">
    
<asp:ListItem Value="0">-请选择-</asp:ListItem>
    
<asp:ListItem Value="ok1">222</asp:ListItem>
    
<asp:ListItem Value="ok2">333</asp:ListItem>
    
<asp:ListItem Value="444">444</asp:ListItem>
    
<asp:ListItem Value="555">555</asp:ListItem>
</asp:DropDownList>

WebUserControl.ascx.vb
Public Class WebUserControl
    
Inherits System.Web.UI.UserControl
    
Private PropertyValue As String


#Region 
" Web 窗体设计器生成的代码 "

    '该调用是 Web 窗体设计器所必需的。
    <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()

    
End Sub

    
Protected WithEvents D1 As System.Web.UI.WebControls.DropDownList

    
'注意: 以下占位符声明是 Web 窗体设计器所必需的。
    '不要删除或移动它。
    Private designerPlaceholderDeclaration As System.Object

    
Private Sub Page_Init(ByVal sender As System.ObjectByVal e As System.EventArgs) Handles MyBase.Init
        
'CODEGEN: 此方法调用是 Web 窗体设计器所必需的
        '不要使用代码编辑器修改它。
        InitializeComponent()
    
End Sub


#
End Region
    
    
Public Property dtext() As String
        
Get
            
Return PropertyValue
        
End Get
        
Set(ByVal Value As String)
            PropertyValue 
= Value
        
End Set
    
End Property



    
Private Sub Page_Load(ByVal sender As System.ObjectByVal e As System.EventArgs) Handles MyBase.Load
        
'在此处放置初始化页的用户代码
    End Sub


    
Private Sub D1_SelectedIndexChanged(ByVal sender As System.ObjectByVal e As System.EventArgs) Handles D1.SelectedIndexChanged
        PropertyValue 
= D1.SelectedItem.Value.ToString()
    
End Sub

End Class



在页面上引用用户控件,取得属性值并将取得的属性值显示于文本框中:uc.aspx
<%@ Register TagPrefix="uc1" TagName="WebUserControl" Src="WebUserControl.ascx" %>
<%@ Page Language="vb" AutoEventWireup="false" Codebehind="uc.aspx.vb" Inherits="test.uc"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML>
    
<HEAD>
        
<title>uc</title>
        
<meta content="Microsoft Visual Studio .NET 7.1" name="GENERATOR">
        
<meta content="Visual Basic .NET 7.1" name="CODE_LANGUAGE">
        
<meta content="JavaScript" name="vs_defaultClientScript">
        
<meta content="http://schemas.microsoft.com/intellisense/ie5" name="vs_targetSchema">
    
</HEAD>
    
<body MS_POSITIONING="GridLayout">
        
<form id="Form1" method="post" runat="server">
            
<asp:button id="Button1" style="Z-INDEX: 101; LEFT: 16px; POSITION: absolute; TOP: 128px" runat="server"
                Text
="Button"></asp:button>
            
<asp:TextBox id="T1" style="Z-INDEX: 102; LEFT: 16px; POSITION: absolute; TOP: 72px" runat="server"></asp:TextBox>
            
<uc1:WebUserControl id="Uc1" runat="server"></uc1:WebUserControl></form>
    
</body>
</HTML>

uc.aspx.vb
Public Class uc
    
Inherits System.Web.UI.Page

#Region 
" Web 窗体设计器生成的代码 "

    '该调用是 Web 窗体设计器所必需的。
    <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()

    
End Sub

    
Protected WithEvents Button1 As System.Web.UI.WebControls.Button
    
Protected WithEvents T1 As System.Web.UI.WebControls.TextBox

    
'注意: 以下占位符声明是 Web 窗体设计器所必需的。
    '不要删除或移动它。
    Private designerPlaceholderDeclaration As System.Object

    
Private Sub Page_Init(ByVal sender As System.ObjectByVal e As System.EventArgs) Handles MyBase.Init
        
'CODEGEN: 此方法调用是 Web 窗体设计器所必需的
        '不要使用代码编辑器修改它。
        InitializeComponent()
    
End Sub


#
End Region
    
Protected uc1 As New WebUserControl
    
Private Sub Page_Load(ByVal sender As System.ObjectByVal e As System.EventArgs) Handles MyBase.Load
        
'在此处放置初始化页的用户代码
    End Sub


    
Private Sub Button1_Click(ByVal sender As System.ObjectByVal e As System.EventArgs) Handles Button1.Click
        T1.Text 
= uc1.dtext
    
End Sub

End Class



posted on 2005-03-18 11:26  lhx  阅读(1229)  评论(0编辑  收藏  举报