events from dynamically loaded control

Discussion in 'ASP .Net' started by karthick raja, Jul 20, 2004.

  1. 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, Jul 20, 2004
    #1
    1. Advertising

  2. 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:
    >NNTP-Posting-Date: Tue, 20 Jul 2004 02:06:27 +0000 (UTC)
    >Path:

    cpmsftngxa06.phx.gbl!TK2MSFTNGP08.phx.gbl!newsfeed00.sul.t-online.de!t-onlin
    e.de!fr.ip.ndsoftware.net!proxad.net!postnews2.google.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).
    >
    Jim Cheshire [MSFT], Jul 20, 2004
    #2
    1. Advertising

  3. 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!
    karthick raja, Jul 20, 2004
    #3
  4. 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.aspx?scid=KB;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:
    >NNTP-Posting-Date: Tue, 20 Jul 2004 14:59:50 +0000 (UTC)
    >Path:

    cpmsftngxa06.phx.gbl!TK2MSFTNGXS01.phx.gbl!TK2MSFTNGXA05.phx.gbl!TK2MSFTNGP0
    8.phx.gbl!news-out.cwix.com!newsfeed.cwix.com!news.maxwell.syr.edu!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], Jul 20, 2004
    #4
  5. 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:
    >NNTP-Posting-Date: Tue, 20 Jul 2004 18:11:42 +0000 (UTC)
    >Path:

    cpmsftngxa06.phx.gbl!TK2MSFTNGP08.phx.gbl!newsfeed00.sul.t-online.de!t-onlin
    e.de!tiscali!newsfeed1.ip.tiscali.net!proxad.net!postnews2.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?
    >
    Jim Cheshire [MSFT], Jul 20, 2004
    #5
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Danny Bloodworth

    Events in dynamically loaded user controls

    Danny Bloodworth, Nov 20, 2003, in forum: ASP .Net
    Replies:
    2
    Views:
    541
  2. Peter Jaffe
    Replies:
    0
    Views:
    1,185
    Peter Jaffe
    Feb 4, 2004
  3. Earl Teigrob
    Replies:
    1
    Views:
    7,692
    nfedin
    Feb 10, 2004
  4. Raed Sawalha
    Replies:
    1
    Views:
    605
    Karl Seguin
    Oct 7, 2004
  5. =?Utf-8?B?Smlt?=
    Replies:
    2
    Views:
    601
    =?Utf-8?B?Smlt?=
    Aug 20, 2007
Loading...

Share This Page