来自ASCX控件的类继承

时间:2015-03-17 11:29:19

标签: asp.net vb.net

我在继承附加了ASCX标记页的类时遇到问题。看,我写了以下ASCX标记和Codebehind:

ASCX

<%@ Control Language="vb" AutoEventWireup="false" CodeBehind="HeaderBlock.ascx.vb" Inherits="CentralWeb.EMP.UI.HeaderBlock" %>
<h1>
    <abbr><%= Me.Label></abbr>
    <span>
        <asp:Button ID="saveandreturnbutton" Text="Salvar e Voltar..." runat="server" CssClass="saveandback" />
        <asp:Button ID="savebutton" Text="Salvar..." runat="server" CssClass="save" />
        <asp:Button ID="return_results" Text="Voltar..." runat="server" CssClass="back" />
    </span>
</h1>

代码隐藏

Namespace EMP.UI
    Public Class HeaderBlock
        Inherits System.Web.UI.WebControls.UserControl
        Protected saveandreturnbutton As Global.System.Web.UI.WebControls.Button
        Protected savebutton As Global.System.Web.UI.WebControls.Button
        Protected return_results As Global.System.Web.UI.WebControls.Button

        Public Overridable Sub TakeAction(ByVal eventId As Integer)
            'NOTHING, THIS IS THE BASE CLASS
        End Sub

        Private Sub saveandreturn_Click(ByVal sender As Object, ByVal e As Global.System.EventArgs) Handles saveandreturnbutton.Click
            Me.TakeAction(3)
        End Sub

        Private Sub savebutton_Click(ByVal sender As Object, ByVal e As Global.System.EventArgs) Handles savebutton.Click
            Me.TakeAction(2)
        End Sub

        Private Sub return_results_Click(ByVal sender As Object, ByVal e As Global.System.EventArgs) Handles return_results.Click
            Me.TakeAction(1)
        End Sub
    End Class
End Namespace

然后,我的目的是使用这个基类(有一个标记),并将它继承到一个非标记的类,并在页面上引用它。像这样:

Namespace EMP.UI
    Public Class FrontHeader
        Inherits EMP.UI.HeaderBlock

        Public Overrides Sub TakeAction(ByVal I As Integer)
            'CHILD CLASS ACTIONS HERE
        End Sub
    End Class
End Namespace

该测试出了什么问题?

在和蒂姆说话之后,我想做这样的事情:

<%@ Control Language="vb" AutoEventWireup="false" CodeBehind="HeaderBlock.ascx.vb" Inherits="CentralWeb.EMP.UI.HeaderBlock" %>
<h1>
    <abbr><%= Me.Label></abbr>
    <span>
        <asp:Button ID="saveandreturnbutton" Text="Salvar e Voltar..." runat="server" CssClass="saveandback" />
        <asp:Button ID="savebutton" Text="Salvar..." runat="server" CssClass="save" />
        <asp:Button ID="return_results" Text="Voltar..." runat="server" CssClass="back" />
    </span>
</h1>
<div class="fields">
    PLACE HERE CONTROLS DEPENDING ON THE PAGE WHERE THIS CLASS IS...
</div>

1 个答案:

答案 0 :(得分:1)

[免责声明:原始问题没有Inherits System.Web.UI.UserControl]

您需要继承UserControl

Public Class HeaderBlock
    Inherits System.Web.UI.UserControl

 ....

但总的来说,我不会从UserControl继承一个类,因为这是一个不应该包含太多业务逻辑的前端控件。如果你想&#34;延伸&#34;你可以简单地在其中添加另一个UserControlUserControl可以包含一个或多个其他控件,因此是模块化概念。