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

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

WebUserControl.ascx.vb
ExpandedBlockStart.gif ContractedBlock.gif Public   Class WebUserControl Class WebUserControl
InBlock.gif    
Inherits System.Web.UI.UserControl
InBlock.gif    
Private PropertyValue As String
InBlock.gif
InBlock.gif
InBlock.gif#Region 
" Web 窗体设计器生成的代码 "
InBlock.gif

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

InBlock.gif    
Protected WithEvents D1 As System.Web.UI.WebControls.DropDownList
InBlock.gif
InBlock.gif    
'注意: 以下占位符声明是 Web 窗体设计器所必需的。
InBlock.gif
    '不要删除或移动它。
InBlock.gif
    Private designerPlaceholderDeclaration As System.Object
InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif    
Private Sub Page_Init()Sub Page_Init(ByVal sender As System.ObjectByVal e As System.EventArgs) Handles MyBase.Init
InBlock.gif        
'CODEGEN: 此方法调用是 Web 窗体设计器所必需的
InBlock.gif
        '不要使用代码编辑器修改它。
InBlock.gif
        InitializeComponent()
ExpandedSubBlockEnd.gif    
End Sub

InBlock.gif
InBlock.gif#
End Region
InBlock.gif    
ExpandedSubBlockStart.gifContractedSubBlock.gif    
Public Property dtext()Property dtext() As String
InBlock.gif        
Get
InBlock.gif            
Return PropertyValue
InBlock.gif        
End Get
InBlock.gif        
Set(ByVal Value As String)
InBlock.gif            PropertyValue 
= Value
InBlock.gif        
End Set
ExpandedSubBlockEnd.gif    
End Property

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

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

ExpandedBlockEnd.gif
End Class

None.gif


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

uc.aspx.vb
ExpandedBlockStart.gif ContractedBlock.gif Public   Class uc Class uc
InBlock.gif    
Inherits System.Web.UI.Page
InBlock.gif
InBlock.gif#Region 
" Web 窗体设计器生成的代码 "
InBlock.gif

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

InBlock.gif    
Protected WithEvents Button1 As System.Web.UI.WebControls.Button
InBlock.gif    
Protected WithEvents T1 As System.Web.UI.WebControls.TextBox
InBlock.gif
InBlock.gif    
'注意: 以下占位符声明是 Web 窗体设计器所必需的。
InBlock.gif
    '不要删除或移动它。
InBlock.gif
    Private designerPlaceholderDeclaration As System.Object
InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif    
Private Sub Page_Init()Sub Page_Init(ByVal sender As System.ObjectByVal e As System.EventArgs) Handles MyBase.Init
InBlock.gif        
'CODEGEN: 此方法调用是 Web 窗体设计器所必需的
InBlock.gif
        '不要使用代码编辑器修改它。
InBlock.gif
        InitializeComponent()
ExpandedSubBlockEnd.gif    
End Sub

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

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

ExpandedBlockEnd.gif
End Class

None.gif


转载于:https://www.cnblogs.com/lhxhappy/archive/2005/03/18/120986.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值