如何捕获usercontrol事件?

时间:2014-03-01 06:04:39

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

我的所有其他usercontrol的{​​{1}}为container,但在某种程度上,容器forms中引发的events会丢失。

我有一个usercontrol如果我只是把它放在一个页面上(不在我的容器usercontrol button中),我可以处理它的事件。但是,从容器中,标准usercontrol标记中既不处理event也不处理event。 非常感谢!

以下是代码:

asp:button页:

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 的{​​{1}}:

ascx

最后是容器user control的代码隐藏:

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

2 个答案:

答案 0 :(得分:0)

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

' 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

答案 1 :(得分:0)

好的,如果有人需要这个,那就是: -my btnClick事件未被触发,因为我在页面上添加了控件(onprerender)。 固定: -added控件OnInit事件,一切都很好!