Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > ASP .Net > events from dynamically loaded control

Reply
Thread Tools

events from dynamically loaded control

 
 
karthick raja
Guest
Posts: n/a
 
      07-20-2004
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).
 
Reply With Quote
 
 
 
 
Jim Cheshire [MSFT]
Guest
Posts: n/a
 
      07-20-2004
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


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

--------------------
>From: (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: < >
>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-
>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).
>


 
Reply With Quote
 
 
 
 
karthick raja
Guest
Posts: n/a
 
      07-20-2004
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!
 
Reply With Quote
 
Jim Cheshire [MSFT]
Guest
Posts: n/a
 
      07-20-2004
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


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

--------------------
>From: (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: < >
>References: < >
>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-
>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!
>


 
Reply With Quote
 
Jim Cheshire [MSFT]
Guest
Posts: n/a
 
      07-20-2004
The events fire whether you add code to them or not.

Jim Cheshire [MSFT]
MCP+I, MCSE, MCSD, MCDBA
Microsoft Developer Support


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

--------------------
>From: (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: < >
>References: < >

< >
>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-
>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?
>


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
click events no longer work on dynamically loaded web user control =?Utf-8?B?Smlt?= ASP .Net 2 08-20-2007 03:32 AM
Firing Events of dynamically loaded User Control Raed Sawalha ASP .Net 1 10-07-2004 12:38 PM
Dynamically loaded user control events not firing on first click Earl Teigrob ASP .Net 1 02-10-2004 03:10 PM
Re: Problems with PostBack events when swapping dynamically loaded controls in a page Peter Jaffe ASP .Net 0 02-04-2004 12:06 PM
Events in dynamically loaded user controls Danny Bloodworth ASP .Net 2 11-21-2003 07:23 AM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57