Ok so if anybody needs this here it is: -my btnClick event was not triggered because i was adding the controls on the page to late (onprerender). FIX: -added controls OnInit event and everything works great!
How to catch usercontrol event?
-
18-10-2022 - |
Question
I have a usercontrol
that is a container
for all my other forms
but somehow the events
raised within the container usercontrol
are lost.
I have a usercontrol button
and if I just put it on a page (not within my container usercontrol
) i can handle its events. However, from within the container, neither event
is handled nor event
from a standard asp:button
tag.
Thanks a lot!
Here is is the code:
Main aspx
page:
<%@ 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>
Code behind for main aspx
page:
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
And the ascx
for the user control
:
<%@ Control Language="vb" AutoEventWireup="false" CodeBehind="Form.ascx.vb" Inherits="Comanda.Form" %>
And finally the code-behind for the container 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
Solution 2
OTHER TIPS
Your usercontrol isn't raising an events. Somewhere in your usercontrol you need to define what events you want to raise:
' Declare events at module level.
Event TitleChanged() ' No parameters
Event MySizeChanged(ByVal Width As Double, ByVal Height As Double)
Then add the code to Raise the event in the appropriate place:
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