Reputation: 154
Its been 4 hours since i thought "How can i trigger my event from a usercontrol's button click?" and the answer came very fast : "i can caputre the button's click event and raise mine from there!" and after 4 hours of debugging and debugging and more debugging and page lifecycle and more debugging and more breakpoints and so on and so forth i turn for help to the GREAT STACKOVERFLOW(bows). So the question is quite straightforward; Why cant i capture the onclick event of a button placed inside a usercontrol so i can trigger my event? As for the code : Here is the ascx :
<%@ Control Language="vb" AutoEventWireup="true" CodeBehind="CosmaButton.ascx.vb" Inherits="Comanda.CosmaButton" %>
Code behind for the usercontrol :
Public Class CosmaButton
Inherits System.Web.UI.UserControl
Public Event ClickBtn As EventHandler
Private _title As String = "test"
Private _width As Integer
Private WithEvents _button As New Button
Protected Overrides Sub OnInit(e As EventArgs)
MyBase.OnInit(e)
End Sub
Protected Overrides Sub OnLoad(e As EventArgs)
MyBase.OnLoad(e)
End Sub
Protected Overrides Sub OnPreRender(e As EventArgs)
_button.CssClass = "styled"
_button.Text = _title
_button.Attributes("runat") = "server"
If _width > 0 Then
_button.Width = _width
End If
Me.Controls.Add(_button)
MyBase.OnPreRender(e)
End Sub
Protected Sub _button_Clicked(sender As Object, e As EventArgs)
RaiseEvent ClickBtn(Nothing, Nothing)
End Sub
End Class
and finally here is the codebehind for the 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
AddHandler btn.ClickBtn, AddressOf btn_Click
End Sub
Protected Sub btn_Click(sender As Object, e As EventArgs) Handles btn.ClickBtn
End Sub
End Class
PS did my homework and searched every post that related to this isssue and found : http://www.codeproject.com/Articles/33874/Capture-Button-s-Click-Event-of-User-Control-in-th which is kind of frustrating because i followed and refollowed and went step by step in this guy's post but the same effect. No freaking clickevent!
EDIT 1:
added handler to click event so the updated codebehind version is the following:
Public Class CosmaButton
Inherits System.Web.UI.UserControl
Public Event ClickBtn As EventHandler
Private _title As String = "test"
Private _width As Integer
Private WithEvents _button As New Button
Protected Overrides Sub OnInit(e As EventArgs)
Me.Controls.Add(_button)
AddHandler _button.Click, AddressOf _button_Clicked
MyBase.OnInit(e)
End Sub
Protected Overrides Sub OnLoad(e As EventArgs)
MyBase.OnLoad(e)
End Sub
Protected Overrides Sub OnPreRender(e As EventArgs)
_button.CssClass = "styled"
_button.Text = _title
_button.Attributes("runat") = "server"
If _width > 0 Then
_button.Width = _width
End If
MyBase.OnPreRender(e)
End Sub
Protected Sub _button_Clicked(sender As Object, e As EventArgs)
RaiseEvent ClickBtn(Nothing, Nothing)
End Sub
End Class
and still nothing. I added the handler many times before but after reading and trying the post up mentioned, i added an asp:button tag to my usercontrol but with no success, and after that i posted the question.
Upvotes: 0
Views: 2380
Reputation: 2793
If I understand correctly (with my not so good knowledge of VB.net) you are dynamically adding a button to your user control. But the button click event is never bound. If you just add a handler for button click event on your user control, things would probably work for you.
Another piece of advice when ever playing with dynamic controls always try to add control on page init
or preinit
so you can make use of the advantages of viewstate
.
Edit 1:
I have created a sample for you, that would help you solve the issue. For your user control you can place the button in designer itself (here is what I did): [User Control Designer]
<asp:Button runat="server" ID="button" />
Now on the code behind of the user control, I added the following code: [User Control Code Behind]
Public Event AnEvent()
Protected Sub button_Click(sender As Object, e As EventArgs) Handles button.Click
RaiseEvent AnEvent()
End Sub
botton_Click is the click event of the button added on your user control. The click of this button raises AnEvent. Now this makes your user control a full package having it's own event.
The only thing left to do is add this user control to your page and handle the AnEvent. The following two lines on the page added the user control and handeled the event for me: [Web Page Designer (Adding user control)]
<%@ Register src="WebUserControl1.ascx" tagname="WebUserControl1" tagprefix="uc1" %>
<uc1:WebUserControl1 ID="WebUserControl11" runat="server" OnAnEvent="WebUserControl11_AnEvent" />
Hope this helps.
Upvotes: 1
Reputation: 170
Shashank's right. Here somebody wrote something similar
ASP.NET dynamic Command Button event not firing
Apparently, your code lacks the instruction
AddHandler _button.Click, AddressOf btn_Click
to add a handler for button click, and remove
Protected Sub _button_Clicked(sender As Object, e As EventArgs)
RaiseEvent ClickBtn(Nothing, Nothing)
End Sub
Upvotes: 1