Hello to everyone, How are you?
Have a great day.
I'm trying to code a Web Solution to add some WebUserControls dinamically and cacht their Click Event. I read some forums like this and I found out that for that it was necesary to Delegate my click event from my Control and reasign thru
AddHandler and
AddressOf instructions on my "Default.aspx" file.
Well, to this step everything goes fine. But when I click any button from my Control I got a System.InvalidCastException Error Message (HResult=0x80004002) that says that is not possible convert a "System.Web.UI.HtmlControls.HtmlGenericControlobject" Object Type to "MyControl" Object.
It makes reference to an especific instrucction on my click event from my "Default.aspx" file:
Dim laFiguraSeleccioanda As MiControl = DirectCast(DirectCast(sender, Button).Parent, MiControl)
And that is a call from my click event from My Control file with this instrucction:
RaiseEvent EventoPublico_Click(sender, e)
Can some one please help me? How can I catch click event from MyControl file and I do something declarated on my Default page?
I let all of you my code built on ASPX.VB so you can have a better idea what I mean.
Here is a Screen Shot --->
ScreenShot
Wishing you all an excellent day, I reiterate my greeting and thank you in advance for your kind attention and help.
What I have tried:
Default.aspx
<%@ Page Language="VB" AutoEventWireup="false" CodeFile="Default.aspx.vb" Inherits="_Default" %>
<%@ Register Src="~/App_Controls/MiControl.ascx" TagPrefix="CJS" TagName="Figura" %>
<head runat="server">
<title>Evento click con delegado</title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<link href="StyleSheet.css" type="text/css" rel="stylesheet" />
</head>
<body>
<form id="form1" runat="server">
<div runat="server">
<asp:PlaceHolder ID="PlaceHolder1" runat="server"></asp:PlaceHolder>
</div>
<br />
<div runat="server" class="Texto" style="clear:left">
<asp:Label ID="lbResultado" runat="server" Text="Esperando un Click.."></asp:Label>
</div>
</form>
</body>
Default.aspx.vb
Partial Class _Default
Inherits System.Web.UI.Page
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles form1.Load
For i As Integer = 1 To 3
Dim laFigura As MiControl = LoadControl("~/App_Controls/MiControl.ascx")
AddHandler laFigura.EventoPublico_Click, AddressOf LaFigura_Click
PlaceHolder1.Controls.Add(laFigura)
laFigura.SuNombre = "Cuadrito 0" & i
Next
End Sub
Protected Sub LaFigura_Click(ByVal sender As Object, ByVal e As EventArgs)
Dim laFiguraSeleccioanda As MiControl = DirectCast(DirectCast(sender, Button).Parent, MiControl)
lbResultado.Text = "Hiciste click en el " & laFiguraSeleccioanda.SuNombre
End Sub
End Class
MiControl.ascx
<%@ Control Language="VB" AutoEventWireup="false" CodeFile="MiControl.ascx.vb" Inherits="MiControl" %>
<link href="../StyleSheet.css" type="text/css" rel="stylesheet" />
<asp:Panel ID="Cuadrito" CssClass="Cuadrito Centro" runat="server">
<div runat="server">
<asp:Label ID="lbCuadrito" CssClass="Texto" Text="Control" runat="server" ></asp:Label>
</div>
<div runat="server">
<asp:Button ID="btnCuadrito" runat="server" Text="Haz click" />
</div>
</asp:Panel>
MiControl.ascx.vb
Partial Class MiControl
Inherits System.Web.UI.UserControl
Public Delegate Sub Delegado_EventoClick(ByVal sender As Object, ByVal e As System.EventArgs)
Public Event EventoPublico_Click As Delegado_EventoClick
Public Property SuNombre As String
Get
Return lbCuadrito.Text
End Get
Set(value As String)
lbCuadrito.Text = value
End Set
End Property
Private Sub btnCuadrito_Click(sender As Object, e As EventArgs) Handles btnCuadrito.Click
RaiseEvent EventoPublico_Click(sender, e)
End Sub
End Class
Error Message:
System.InvalidCastException occurred
HResult=0x80004002
Message=No se puede convertir un objeto de tipo 'System.Web.UI.HtmlControls.HtmlGenericControl' al tipo 'MiControl'.
Source=App_Web_tumekcyd
StackTrace:
at _Default.LaFigura_Click(Object sender, EventArgs e) in D:\Mis documentos\Ejemplo 03 - Evento Click con delegado\Default.aspx.vb:line 14
at MiControl.btnCuadrito_Click(Object sender, EventArgs e) in D:\Mis documentos\Ejemplo 03 - Evento Click con delegado\App_Controls\MiControl.ascx.vb:line 16