events from dynamically loaded control

K

karthick raja

Am experiencing a problem intercepting the events from controls added
dynamically to a Placeholder control at runtime.
Is there any way that I can write an event handler on the page which will be
pick up the events raised by the added controls dynamically.

I cannot use 'WithEvents' and 'Handles' because this requires that the
controls be declared on the page and they aren't (they are created in the
external class)
I did think of using "AddHandler" in the external class, but this requires
that the Handling function be local to the code that adds the controls,
which it isn't(its in the calling page).
 
J

Jim Cheshire [MSFT]

Karthick,

You have to set up an event delegate. AddHandler is the correct method.

Jim Cheshire [MSFT]
MCP+I, MCSE, MCSD, MCDBA
Microsoft Developer Support
(e-mail address removed)

This post is provided "AS-IS" with no warranties and confers no rights.

--------------------
 
K

karthick raja

I found a solution to the above problem. I added an event handler just
after adding the control to the placeholder.


--------
webpage
----------
Protected WithEvents ctrlGeneric As Control
Protected WithEvents phUserCtrl As
System.Web.UI.WebControls.PlaceHolder
.....
Private Sub AddControl()
Me.ctrlGeneric = LoadControl("PRLargeReport.ascx")
Dim ctrlPRLargeReport As PRLargeReportCtrl
ctrlPRLargeReport = CType(ctrlGeneric, PRLargeReportCtrl)
Me.phUserCtrl.Controls.Add(ctrlPRLargeReport)
viewstate("AddedControl") = "True"
AddHandler ctrlPRLargeReport.evShowConfirmation, AddressOf
ctrlPRLargeReport_evShowConfirmation
End Sub


Private Sub ctrlPRLargeReport_evShowConfirmation()
'handling the event here
End Sub

---------
user control
----------

Public Delegate Sub ShowConformation()
Public Event evShowConfirmation As ShowConformation
....
....
Private Sub btnTransfer_Click(ByVal sender As System.Object, ByVal e
As System.EventArgs) Handles btnTransfer.Click
RaiseEvent evShowConfirmation()
end sub


Is there any other way to do this?? I would really appreciate your
possible insights. thanks!
 
J

Jim Cheshire [MSFT]

The events fire whether you add code to them or not.

Jim Cheshire [MSFT]
MCP+I, MCSE, MCSD, MCDBA
Microsoft Developer Support
(e-mail address removed)

This post is provided "AS-IS" with no warranties and confers no rights.

--------------------
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,768
Messages
2,569,575
Members
45,053
Latest member
billing-software

Latest Threads

Top