Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   ASP .Net (http://www.velocityreviews.com/forums/f29-asp-net.html)
-   -   events from dynamically loaded control (http://www.velocityreviews.com/forums/t83080-events-from-dynamically-loaded-control.html)

karthick raja 07-20-2004 02:06 AM

events from dynamically loaded control
 
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).

Jim Cheshire [MSFT] 07-20-2004 02:57 PM

RE: events from dynamically loaded control
 
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
jamesche@online.microsoft.com

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

--------------------
>From: karthickraja@hotmail.com (karthick raja)
>Newsgroups: microsoft.public.dotnet.framework.aspnet
>Subject: events from dynamically loaded control
>Date: 19 Jul 2004 19:06:27 -0700
>Organization: http://groups.google.com
>Lines: 11
>Message-ID: <266a9bbe.0407191806.731113a4@posting.google.com >
>NNTP-Posting-Host: 204.118.77.183
>Content-Type: text/plain; charset=ISO-8859-1
>Content-Transfer-Encoding: 8bit
>X-Trace: posting.google.com 1090289187 31186 127.0.0.1 (20 Jul 2004

02:06:27 GMT)
>X-Complaints-To: groups-abuse@google.com
>NNTP-Posting-Date: Tue, 20 Jul 2004 02:06:27 +0000 (UTC)
>Path:

cpmsftngxa06.phx.gbl!TK2MSFTNGP08.phx.gbl!newsfeed 00.sul.t-online.de!t-onlin
e.de!fr.ip.ndsoftware.net!proxad.net!postnews2.goo gle.com!not-for-mail
>Xref: cpmsftngxa06.phx.gbl microsoft.public.dotnet.framework.aspnet:248295
>X-Tomcat-NG: microsoft.public.dotnet.framework.aspnet
>
>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).
>



karthick raja 07-20-2004 02:59 PM

Re: events from dynamically loaded control
 
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!

Jim Cheshire [MSFT] 07-20-2004 03:20 PM

Re: events from dynamically loaded control
 
Karthick,

Where are you calling AddControl()? You might want to review our
documentation on how to work with dynamic controls:

http://support.microsoft.com/default...B;EN-US;317515

Jim Cheshire [MSFT]
MCP+I, MCSE, MCSD, MCDBA
Microsoft Developer Support
jamesche@online.microsoft.com

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

--------------------
>From: karthickraja@hotmail.com (karthick raja)
>Newsgroups: microsoft.public.dotnet.framework.aspnet
>Subject: Re: events from dynamically loaded control
>Date: 20 Jul 2004 07:59:49 -0700
>Organization: http://groups.google.com
>Lines: 42
>Message-ID: <266a9bbe.0407200659.7984a9a4@posting.google.com >
>References: <266a9bbe.0407191806.731113a4@posting.google.com >
>NNTP-Posting-Host: 162.114.211.143
>Content-Type: text/plain; charset=ISO-8859-1
>Content-Transfer-Encoding: 8bit
>X-Trace: posting.google.com 1090335590 2632 127.0.0.1 (20 Jul 2004

14:59:50 GMT)
>X-Complaints-To: groups-abuse@google.com
>NNTP-Posting-Date: Tue, 20 Jul 2004 14:59:50 +0000 (UTC)
>Path:

cpmsftngxa06.phx.gbl!TK2MSFTNGXS01.phx.gbl!TK2MSFT NGXA05.phx.gbl!TK2MSFTNGP0
8.phx.gbl!news-out.cwix.com!newsfeed.cwix.com!news.maxwell.syr.ed u!postnews2
.google.com!not-for-mail
>Xref: cpmsftngxa06.phx.gbl microsoft.public.dotnet.framework.aspnet:248453
>X-Tomcat-NG: microsoft.public.dotnet.framework.aspnet
>
>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!
>



Jim Cheshire [MSFT] 07-20-2004 07:32 PM

Re: events from dynamically loaded control
 
The events fire whether you add code to them or not.

Jim Cheshire [MSFT]
MCP+I, MCSE, MCSD, MCDBA
Microsoft Developer Support
jamesche@online.microsoft.com

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

--------------------
>From: karthickraja@hotmail.com (karthick raja)
>Newsgroups: microsoft.public.dotnet.framework.aspnet
>Subject: Re: events from dynamically loaded control
>Date: 20 Jul 2004 11:11:42 -0700
>Organization: http://groups.google.com
>Lines: 14
>Message-ID: <266a9bbe.0407201011.74c9715a@posting.google.com >
>References: <266a9bbe.0407191806.731113a4@posting.google.com >

<266a9bbe.0407200659.7984a9a4@posting.google.com >
>NNTP-Posting-Host: 162.114.211.143
>Content-Type: text/plain; charset=ISO-8859-1
>Content-Transfer-Encoding: 8bit
>X-Trace: posting.google.com 1090347102 12841 127.0.0.1 (20 Jul 2004

18:11:42 GMT)
>X-Complaints-To: groups-abuse@google.com
>NNTP-Posting-Date: Tue, 20 Jul 2004 18:11:42 +0000 (UTC)
>Path:

cpmsftngxa06.phx.gbl!TK2MSFTNGP08.phx.gbl!newsfeed 00.sul.t-online.de!t-onlin
e.de!tiscali!newsfeed1.ip.tiscali.net!proxad.net!p ostnews2.google.com!not-fo
r-mail
>Xref: cpmsftngxa06.phx.gbl microsoft.public.dotnet.framework.aspnet:248525
>X-Tomcat-NG: microsoft.public.dotnet.framework.aspnet
>
>Thanks for the fast response Jim. I have one more concern here. I have
>an aspx page in which there are header,footer and navigation bar user
>controls.
>In addition to that, I have added quite a number of user controls ,
>anywhere from 6 to 10. I am showing the user controls by toggling
>their Visible property based on certain conditions.
>
>Even though there is not much of processing done in the Page_init or
>Page_load of these user controls, I am somehow un-comfortable at the
>sight of 10 page init and page load (user control ) + 1 (for web page)
>events fired (even though some user controls might not be used at all)
>when the page is loaded. What would the pros and cons of using one
>placeholder and filling it with the "appropriate" user control based
>on conditions?
>




All times are GMT. The time now is 04:27 PM.

Powered by vBulletin®. Copyright ©2000 - 2014, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.