創建用戶控件

用戶控件

<%  @ Control ClassName = " Hello "   %>
< script language = " vb "  runat = " server " >
Private _name As String
Public Property Name As String
Get
    Return _name
End Get
Set
    _name
= Value
End Set
End Property
Public Sub SayHello()
Label1.Text
= " Hello, "   &  _name  &   " ! "
End Sub
</ Script >
< asp:Label id = Label1 runat = " server " ></ asp:Label >

Web頁面

<%  @ Page Language="vb"  %>
'以聲明方式添加用戶控件
<%  @ Register TagPrefix="aspnetsbs" TagName="Hello" Src="Hello.ascx"  %>
< html >
< head >
< script  runat ="server" >
Sub Page_Load(Sender As Object,E As EventArgs)
MyHello.Name
="HURENFEI"
MyHello.SayHello
End Sub
</ script >
</ head >
< body >
< aspnetsbs:Hello  id ="MyHello"  runat ="server" />
</ body >
</ html >

 

<%  @ Page Language="vb"  %>
'以編程方式添加用戶控件
<%  @ Reference Control="Hello.ascx"  %>
< html >
< head >
< script  runat ="server" >
Sub Page_Load(Sender As Object,E As EventArgs)
Dim MyHello As Control
=LoadControl("Hello.ascx")
HelloHolder.Controls.Add(MyHello)
CType(MyHello,Hello).Name 
="HuRenfei"
CType(MyHello,Hello).SayHello
End Sub
</ script >
</ head >
< body >
< asp:placeholder  id ="HelloHolder"  runat ="server" />
</ body >
</ html >


<% @ Control ClassName="Hello" %>

你可能感兴趣的:(創建用戶控件)