相对于 ServerControl 来说,UserControl 是相当容易上手的,利用拖曳现有控件就可以组合成复杂的控件。
一般 UserControl 的撰写方式跟页面差不多,不过 UserControl 有时需要提供一些事件,让使用这个 UserControl 的页面可以在此事件中撰写相关的程序代码,以下我们就会示范如果撰写 UserControl 的事件。

首先撰写一个 Toolbar 的 UserControl,Toolbar 上具有「首笔」、「上笔」、「下笔」、「末笔」四个按钮,当按下按钮时会引发 Click 事件,并可由事件自变量的 e.CommandName 得知那个按钮引发此事件。

Toolbar.ascx 的程序代码及画面如下
1撰写 UserControl 的事件撰写 UserControl 的事件<%撰写 UserControl 的事件@ Control Language="VB" AutoEventWireup="false" CodeFile="Toolbar.ascx.vb" Inherits="Toolbar" %>
2撰写 UserControl 的事件<asp:Button ID="btnMoveFirst" runat="server" CommandName="MoveFirst" Text="首筆" />
3撰写 UserControl 的事件<asp:Button ID="btnMovePrevious" runat="server" CommandName="MovePrevious" Text="上筆" />
4撰写 UserControl 的事件<asp:Button ID="btnMoveNext" runat="server" CommandName="MoveNext" Text="下筆" />
5撰写 UserControl 的事件<asp:Button ID="btnMoveLast" runat="server" CommandName="MoveLast" Text="末筆" />

撰写 UserControl 的事件

Toolbar.ascx.vb 的程序代码如下
 1撰写 UserControl 的事件撰写 UserControl 的事件Partial Class ToolbarClass Toolbar
 2撰写 UserControl 的事件    Inherits System.Web.UI.UserControl
 3撰写 UserControl 的事件
 4撰写 UserControl 的事件撰写 UserControl 的事件    /**/''' <summary>
 5撰写 UserControl 的事件    ''' Toolbar 控制項的 Click 事件引數。
 6撰写 UserControl 的事件    ''' </summary>

 7撰写 UserControl 的事件撰写 UserControl 的事件    Public Class ClickEventArgsClass ClickEventArgs
 8撰写 UserControl 的事件        Inherits System.EventArgs
 9撰写 UserControl 的事件        Dim FCommandName As String = String.Empty
10撰写 UserControl 的事件
11撰写 UserControl 的事件撰写 UserControl 的事件        /**/''' <summary>
12撰写 UserControl 的事件        ''' 按鈕命令。
13撰写 UserControl 的事件        ''' </summary>

14撰写 UserControl 的事件撰写 UserControl 的事件        Public Property CommandName()Property CommandName() As String
15撰写 UserControl 的事件            Get
16撰写 UserControl 的事件                Return FCommandName
17撰写 UserControl 的事件            End Get
18撰写 UserControl 的事件            Set(ByVal value As String)
19撰写 UserControl 的事件                FCommandName = value
20撰写 UserControl 的事件            End Set
21撰写 UserControl 的事件        End Property

22撰写 UserControl 的事件    End Class

23撰写 UserControl 的事件
24撰写 UserControl 的事件撰写 UserControl 的事件    /**/''' <summary>
25撰写 UserControl 的事件    ''' 按下工具列按鈕引發的事件。
26撰写 UserControl 的事件    ''' </summary>

27撰写 UserControl 的事件    Public Event Click(ByVal sender As ObjectByVal e As ClickEventArgs)
28撰写 UserControl 的事件
29撰写 UserControl 的事件撰写 UserControl 的事件    /**/''' <summary>
30撰写 UserControl 的事件    ''' 引發 Click 事件。
31撰写 UserControl 的事件    ''' </summary>

32撰写 UserControl 的事件撰写 UserControl 的事件    Public Overloads Sub RaiseClickEvent()Sub RaiseClickEvent(ByVal e As ClickEventArgs)
33撰写 UserControl 的事件        RaiseEvent Click(Me, e)
34撰写 UserControl 的事件    End Sub

35撰写 UserControl 的事件
36撰写 UserControl 的事件撰写 UserControl 的事件    /**/''' <summary>
37撰写 UserControl 的事件    ''' 所有按鈕的事件處理函式。
38撰写 UserControl 的事件    ''' </summary>

39撰写 UserControl 的事件撰写 UserControl 的事件    Protected Sub Button_Click()Sub Button_Click(ByVal sender As ObjectByVal e As System.EventArgs) Handles _
40撰写 UserControl 的事件        btnMoveFirst.Click, btnMovePrevious.Click, btnMoveNext.Click, btnMoveLast.Click
41撰写 UserControl 的事件        Dim oEventArgs As New ClickEventArgs()
42撰写 UserControl 的事件
43撰写 UserControl 的事件        '取得作用按鈕的 CommandName
44撰写 UserControl 的事件        oEventArgs.CommandName = CType(sender, Button).CommandName
45撰写 UserControl 的事件        '引發 Click 事件
46撰写 UserControl 的事件        RaiseClickEvent(oEventArgs)
47撰写 UserControl 的事件    End Sub

48撰写 UserControl 的事件
49撰写 UserControl 的事件End Class


在 Toolbar.ascx.vb中定义了 Click 事件,Click 事件自变量为 ClickEventArgs,ClickEventArgs 具有 CommandName 属性,用来识别按钮命令。当 Toolbar 按下按钮时,会进 Button_Click 事件处理函式,在此函式中取得作用按钮的 CommandName,并引发 Toolbar 的 Click 事件。

接下来就是检验成果的时候,将 Toolbar UserControl 拖曳至页面上,切换至页面的程序代码 (*.aspx.vb),在控件清单中选取 Toolbar,在右方的事件清单中,就可以看到在 Toolbar 定义的 Click 事件了。

撰写 UserControl 的事件

在 Toolbar UserControl 的 Click 事件中,撰写如下程序代码,当按下按钮时就会输出对应的 CommandName。

1撰写 UserControl 的事件撰写 UserControl 的事件Partial Class _DefaultClass _Default
2撰写 UserControl 的事件    Inherits System.Web.UI.Page
3撰写 UserControl 的事件
4撰写 UserControl 的事件撰写 UserControl 的事件    Protected Sub Toolbar1_Click()Sub Toolbar1_Click(ByVal sender As ObjectByVal e As Toolbar.ClickEventArgs) Handles Toolbar1.Click
5撰写 UserControl 的事件        Me.Response.Write(e.CommandName)
6撰写 UserControl 的事件    End Sub

7撰写 UserControl 的事件End Class

程序代码下载UserControlEvent.rar

转载于:https://www.cnblogs.com/jeff377/archive/2008/01/19/1045056.html

相关文章:

  • 2021-11-14
  • 2021-12-17
  • 2022-12-23
  • 2021-11-16
  • 2021-10-17
  • 2021-08-09
  • 2022-12-23
猜你喜欢
  • 2021-08-20
  • 2021-08-28
  • 2022-12-23
  • 2021-12-21
  • 2022-01-16
  • 2022-03-07
相关资源
相似解决方案