【问题标题】:How to catch usercontrol event?如何捕获用户控件事件?
【发布时间】:2014-03-01 06:04:38
【问题描述】:

我有一个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

还有ascx 对应user control

<%@ 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

【问题讨论】:

  • 您的问题太长,无法阅读!哈!最好只写出错的部分。
  • @KashishArora 问题是我没有收到任何错误,只是没有引发用户控件中发生的任何事件。

标签: asp.net vb.net user-controls event-handling


【解决方案1】:

您的用户控件没有引发事件。您需要在用户控件中的某处定义要引发的事件:

' 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

【讨论】:

  • 我需要知道我想在表单上使用的按钮或文本框等子控件引发了哪些事件......它没有引发或处理我的问题的自定义事件,它的捕获事件由按钮引发或任何其他服务器控件。例如,如果我向自定义用户控件添加一个按钮,则我可以捕获单击事件并在我的页面中使用它,但是当我尝试从添加到我的用户控件容器上的标准按钮捕获单击事件时,该事件未捕获.
  • 您必须在添加自定义事件的相同方法中公开子事件。
【解决方案2】:

好的,如果有人在这里需要这个,那就是: -我的 btnClick 事件未触发,因为我将页面上的控件添加到后期(onprerender)。 使固定: - 添加控件 OnInit 事件,一切正常!

【讨论】:

    猜你喜欢
    • 2013-05-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-12-22
    • 2010-12-15
    相关资源
    最近更新 更多