我尝试创建自定义控件,从 .vb 代码动态加载。
这是我在文件“controlcar.ascx”中的自定义控件“ControlCar”
<%@ Control Language="VB" ClassName="ControlCar" %>
<script runat="server">
Private m_car As Car = Nothing
Public Property Car() As Car
Get
Car= m_car
End Get
Set(ByVal value As Car)
m_car = value
End Set
End Property
Protected Sub Panel_OnLoad(ByVal sender As Object, ByVal e As System.EventArgs)
If Me.m_car Is Nothing Then
lit_color.Text = "(m_car Is Nothing)"
Else
lit_color.Text = "color of Me.m_car is (" & Me.m_car.Color & ")"
End If
End Sub
</script>
<asp:Panel ID="panel" OnLoad="Panel_OnLoad" runat="server">
this is a car<br />
color = <asp:Literal ID="lit_color" runat="server"></asp:Literal><br />
<br />
</asp:Panel>
这是我在文件“cars.aspx”中的 ASP 网页,它使用 .vb 代码进行事件,...
<%@ Page Language="vb" Explicit="true" Inherits="PageBase" Src="/code/cars.vb"%>
<html>
<body>
<!-- Html code here --->
<asp:panel ID="panel_cars" runat="server">
</asp:panel>
</body>
这是我在文件“cars.vb”中的.vb代码
Private Sub CreateCar()
Dim car As Car = new Car()
Dim control As ControlCar = Nothing
control= CType(LoadControl("/code/controlcar.ascx"), ControlCar)
control.Car = car
panel_cars.Controls.Add(control)
End Sub
但它失败了,说'ControlCar'在cars.vb中无法识别。
我知道它有效,如果在 .aspx 文件中移动 .vb 代码并使用指令
<%@ Register TagPrefix="uc" TagName="ControlCar" Src="/code/controlcar.ascx" %>
但我需要像我的示例中那样将 .vb 代码和 .aspx 代码分开。
如何在 .vb 文件中识别“ControlCar”类型(在 .ascx 中定义)?