如何捕获用户控件事件



我有一个usercontrol,它是我所有其他formscontainer,但不知何故,容器usercontrol内提出的events丢失了。

我有一个usercontrol button,如果我只是把它放在一个页面上(而不是在我的容器usercontrol内),我可以处理它的事件。但是,从容器内部,既不会处理event,也不会从标准asp:button标记event。多谢!

这是代码:

aspx页面:

<%@ Page Language="vb" AutoEventWireup="false" CodeBehind="Login.aspx.vb" Inherits="Comanda.Login1" %>
<%@ Register TagPrefix="Cosma" TagName="Form" Src="~/Form.ascx" %>
<%@ Register TagPrefix="Cosma" TagName="Button" Src="~/CosmaButton.ascx" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<link href="main.css" type="text/css" rel="stylesheet" />
<title></title>
</head>
<body>
<form id="form1" runat="server">
    <div style="text-align: center;">
        <Cosma:Form ID="form" Title="Login" Width="500" Height="400" runat="server">
            <%-- TODO de adaugat buttoane si textboxuri --%>
            <div style="float:left;display:inline-block;margin-top:3%;width:170px; height:80%; border-right:2px solid #42458a;">
                <p>
                    lorem ipsum
                </p>
            </div>
            <asp:Table CssClass="loginTable" runat="server" ID="table">
                <asp:TableRow>
                    <asp:TableCell>
                    <asp:Label Text="Username: " runat="server"></asp:Label>
                    </asp:TableCell>
                    <asp:TableCell>
                        <asp:TextBox ID="username" runat="server"></asp:TextBox><br />
                    </asp:TableCell>
                </asp:TableRow>
                <asp:TableRow>
                    <asp:TableCell>
                        <asp:Label Text="Password: " runat="server"></asp:Label>
                    </asp:TableCell>
                    <asp:TableCell>
                        <asp:TextBox ID="password" runat="server" TextMode="Password"></asp:TextBox><br />
                    </asp:TableCell>
                </asp:TableRow>
                <asp:TableRow>
                    <asp:TableCell ColumnSpan="2">
                        <span style="float: right;">
                        </span>
                    </asp:TableCell>
                </asp:TableRow>
            </asp:Table>
            <div style="float:right; display:inline-block; margin-top:120px;" >
                <asp:Button ID="button" runat="server" Text="Login"/>
            </div>
        </Cosma:Form>
    </div>
</form>

aspx页面的代码隐藏:

Public Class Login1
Inherits System.Web.UI.Page
     Protected Sub Page_Init(sender As Object, e As EventArgs) Handles Me.Init
     End Sub
     Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
     End Sub
     Protected Sub button_Click(sender As Object, e As EventArgs) Handles button.Click
     End Sub
End Class

以及user control ascx

<%@ Control Language="vb" AutoEventWireup="false" CodeBehind="Form.ascx.vb" Inherits="Comanda.Form" %>

最后是容器的代码隐藏usercontrol

<ParseChildren(True, "ChildControls")>
Public Class Form
Inherits System.Web.UI.UserControl
    'TODO de ve nu merge sa raise event?
     Private WithEvents _panel As Panel
     Private _title As String
     Private _width As Double = 0D
     Private _height As Double = 0D
     Private _cssClass As String = "form"
Public Property Title As String
    Get
        Return _title
    End Get
    Set(value As String)
        If value IsNot "" Then
            _title = value
        End If
    End Set
End Property
Public Property Width As Double
    Get
        Return _width
    End Get
    Set(value As Double)
        If value > 0 Then
            _width = value
        End If
    End Set
End Property
Public Property ChildControls As New List(Of Control)
Public Property Height As Double
    Get
        Return _height
    End Get
    Set(value As Double)
        If value > 0 Then
            _height = value
        End If
    End Set
End Property
Public Sub New()
    'empty constructor
End Sub
Public Sub New(title As String)
    _title = title
End Sub

Protected Overrides Sub OnPreRender(e As EventArgs)
    EnsureChildControls()
    _panel = New Panel()
    _panel.Attributes("class") = _cssClass
    If _width > 0 Then
        _panel.Width = New Unit(_width, UnitType.Pixel)
    End If
    If _height > 0 Then
        _panel.Height = New Unit(_height, UnitType.Pixel)
    End If
    Dim titlePanel As New Panel()
    titlePanel.Width = _panel.Width
    titlePanel.Height = New Unit(50, UnitType.Pixel)
    titlePanel.Style("background-color") = "#42458A"
    titlePanel.Style("text-align") = "left"
    Dim label As New Label()
    label.Text = _title
    label.ForeColor = Drawing.Color.White
    label.Style("margin-left") = "10px"
    titlePanel.Controls.Add(label)
    _panel.Controls.Add(titlePanel)
    For Each c As Control In ChildControls
        _panel.Controls.Add(c)
    Next
    Me.Controls.Add(_panel)
    MyBase.OnPreRender(e)
End Sub
End Class

您的用户控件不会引发事件。在用户控件中的某个位置,您需要定义要引发的事件:

' Declare events at module level.
Event TitleChanged()  ' No parameters
Event MySizeChanged(ByVal Width As Double, ByVal Height As Double)

然后将代码添加到适当的位置引发事件:

Public Property Title As String
    Get
        Return _title
    End Get
    Set(value As String)
        If value IsNot "" Then
            _title = value
            RaiseEvent TitleChanged()
        End If
    End Set
End Property
Public Property Width As Double
    Get
        Return _width
    End Get
    Set(value As Double)
        If value > 0 Then
            _width = value
            RaiseEvent MySizeChanged(Me.Width, Me.Height)
        End If
    End Set
End Property
好的,

所以如果有人需要这个,这里是:-我的 btnClick 事件没有被触发,因为我正在将页面上的控件添加到后期(onprerender)。修复:- 添加了控件OnInit事件,一切正常!

相关内容

  • 没有找到相关文章

最新更新