自定义控件中的控件调用引用控件的页面里的函数

时间:2022-11-09 16:43:42

Imports System.ComponentModel<?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" />

Imports System.Web.UI

Namespace WebControls

    <DefaultProperty("Text"), ToolboxData("<{0}: CustomToolBar runat=server></{0}:CustomToolBar>")> Public Class CustomToolBar

        Inherits System.Web.UI.WebControls.WebControl

'定义3按钮

        Public WithEvents btnAdd As System.Web.UI.WebControls.Button

        Public WithEvents btnDelete As System.Web.UI.WebControls.Button

        Public WithEvents btnEdit As System.Web.UI.WebControls.Button 

Private Sub Button_Ini(ByRef objButton As Button, ByVal Text As String)

objButton = New System.Web.UI.WebControls.Button

objButton.Text = Text

            Me.Controls.Add(objButton)

End Sub     

Public Sub New()

            Button_Ini(btnAddNew, "新增")

            Button_Ini(btnDeleteData, "删除")

End Sub

   End Class

End Namespace

/////////////////////////////////////

为了简明,没在控件里加什么事件、方法、属性之类的。把它编译成dll

调用方法:

新建一页面test.aspx,把控件添加进去。控件Id为CustomToolBar。在test.aspx.vb里的 Page_Init过程里添加:

Public Class frmFabricItem

    Inherits System.Web.UI.Page

    Protected DbaControls As AMMS.DbaControls

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

    '该调用是 Web 窗体设计器所必需的。

    <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()

    End Sub

    Private Sub Page_Init(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Init

        'CODEGEN: 此方法调用是 Web 窗体设计器所必需的

        '不要使用代码编辑器修改它。

        InitializeComponent()

        AddHandler CustomToolBar. btnAdd.Click, AddressOf Me.btnAdd_Click

        AddHandler CustomToolBar. btnDelete.Click, AddressOf Me.btnDelete_Click

        AddHandler CustomToolBar. btnEdit.Click, AddressOf Me.btnEdit_Click

    End Sub

#End Region

 

    Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

        '在此处放置初始化页的用户代码

End Sub

'定义btnAdd_Click过程

Private Sub btnAdd_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)

……..

End Sub

'定义btnDelete_Click过程

Private Sub btnDelete_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)

……..

End Sub

'定义btnAdd _Click过程

Private Sub btnEdit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)

……..

End Sub

 

End Class

/////////////////////////////////

其中关键是:AddHandler 语句。 还有要放在Page_Init过程里。

附:

AddHandler 语句

请参见

RemoveHandler 语句 | Handles | 事件和事件处理程序 | AddHandler RemoveHandler

将事件与事件处理程序相关联。

AddHandler event, AddressOf eventhandler

各部分说明

event

要处理的事件的名称。

eventhandler

将处理事件的过程的名称。

备注

AddHandler RemoveHandler 语句使您可以在程序执行过程中的任何时候启动和停止事件处理。