Programmatically listening for an event?

Discussion in 'ASP .Net Web Controls' started by George Leithead, Jul 12, 2006.

  1. Hi,

    I have the following in an ASPX page, that is an event listener, that
    works fine.

    BasicSearch1.SearchActivated += new
    myWebControlLibrary.BasicSearch.SearchEventHandler(SearchResults1.PerformSearch);

    However, I would like to change the ASPX page to not assume the name of
    the control, but instead listen to the event programmatically. I tried
    the below, which does compile and resolve correctly, however, it does
    not appear to be 'listening' to the event and does not get triggered!

    foreach (Control ctrl in Page.Controls)
    {
    if (ctrl.GetType() ==
    typeof(myWebControlLibrary.BasicSearch))
    {
    // This SHOULD have worked! But it doesnt appear to
    indicate that it is listening!
    ((myWebControlLibrary.BasicSearch)ctrl).SearchActivated += new
    WebControlLibrary.BasicSearch.SearchEventHandler(SearchResults1.PerformSearch);
    }
    }
     
    George Leithead, Jul 12, 2006
    #1
    1. Advertising

  2. hi George, you have to go deeper in the hierarchy. The control you are
    looking for may not be a direct child of your pages control collection but a
    child control of the form control in the page. If the control you are
    looking for is further nested within another control, then its up to you to
    dig further down the hierarchy and find it.
    Currently the code is not executing because you are searching in the wrong
    place where the control is not contained.

    follow the tutorial here :
    http://msdn2.microsoft.com/en-us/library/yt340bh4.aspx

    Alessandro Zifiglio
    http://www.AsyncUI.net
    "George Leithead" <> ha scritto nel messaggio
    news:...
    > Hi,
    >
    > I have the following in an ASPX page, that is an event listener, that
    > works fine.
    >
    > BasicSearch1.SearchActivated += new
    > myWebControlLibrary.BasicSearch.SearchEventHandler(SearchResults1.PerformSearch);
    >
    > However, I would like to change the ASPX page to not assume the name of
    > the control, but instead listen to the event programmatically. I tried
    > the below, which does compile and resolve correctly, however, it does
    > not appear to be 'listening' to the event and does not get triggered!
    >
    > foreach (Control ctrl in Page.Controls)
    > {
    > if (ctrl.GetType() ==
    > typeof(myWebControlLibrary.BasicSearch))
    > {
    > // This SHOULD have worked! But it doesnt appear to
    > indicate that it is listening!
    > ((myWebControlLibrary.BasicSearch)ctrl).SearchActivated += new
    > WebControlLibrary.BasicSearch.SearchEventHandler(SearchResults1.PerformSearch);
    > }
    > }
    >
     
    Alessandro Zifiglio, Jul 12, 2006
    #2
    1. Advertising

  3. Alessandro,

    That was it ...spot on. Thanks.

    In the interest of information sharing, my code ended up as:

    foreach (Control ctrl in Page.Controls)
    {
    foreach (Control chldControl in ctrl.Controls)
    {
    if (chldControl.GetType() ==
    typeof(myWebControlLibrary.SearchResults))
    ((myWebControlLibrary.SearchResults)chldControl).PerformSearch(this,
    new SearchActivatedEventArgs(queryExpression, coveoInstanceName,
    coveoCollectionNames, useWildcards));
    }
    }


    George

    Alessandro Zifiglio wrote:

    > hi George, you have to go deeper in the hierarchy. The control you are
    > looking for may not be a direct child of your pages control collection but a
    > child control of the form control in the page. If the control you are
    > looking for is further nested within another control, then its up to you to
    > dig further down the hierarchy and find it.
    > Currently the code is not executing because you are searching in the wrong
    > place where the control is not contained.
    >
    > follow the tutorial here :
    > http://msdn2.microsoft.com/en-us/library/yt340bh4.aspx
    >
    > Alessandro Zifiglio
    > http://www.AsyncUI.net
    > "George Leithead" <> ha scritto nel messaggio
    > news:...
    > > Hi,
    > >
    > > I have the following in an ASPX page, that is an event listener, that
    > > works fine.
    > >
    > > BasicSearch1.SearchActivated += new
    > > myWebControlLibrary.BasicSearch.SearchEventHandler(SearchResults1.PerformSearch);
    > >
    > > However, I would like to change the ASPX page to not assume the name of
    > > the control, but instead listen to the event programmatically. I tried
    > > the below, which does compile and resolve correctly, however, it does
    > > not appear to be 'listening' to the event and does not get triggered!
    > >
    > > foreach (Control ctrl in Page.Controls)
    > > {
    > > if (ctrl.GetType() ==
    > > typeof(myWebControlLibrary.BasicSearch))
    > > {
    > > // This SHOULD have worked! But it doesnt appear to
    > > indicate that it is listening!
    > > ((myWebControlLibrary.BasicSearch)ctrl).SearchActivated += new
    > > WebControlLibrary.BasicSearch.SearchEventHandler(SearchResults1.PerformSearch);
    > > }
    > > }
    > >
     
    George Leithead, Jul 12, 2006
    #3
  4. You are more than welcome George,
    Regards,
    Alessandro Zifiglio
    http://www.AsyncUI.net

    "George Leithead" <> ha scritto nel messaggio
    news:...
    > Alessandro,
    >
    > That was it ...spot on. Thanks.
    >
    > In the interest of information sharing, my code ended up as:
    >
    > foreach (Control ctrl in Page.Controls)
    > {
    > foreach (Control chldControl in ctrl.Controls)
    > {
    > if (chldControl.GetType() ==
    > typeof(myWebControlLibrary.SearchResults))
    > ((myWebControlLibrary.SearchResults)chldControl).PerformSearch(this,
    > new SearchActivatedEventArgs(queryExpression, coveoInstanceName,
    > coveoCollectionNames, useWildcards));
    > }
    > }
    >
    >
    > George
    >
    > Alessandro Zifiglio wrote:
    >
    >> hi George, you have to go deeper in the hierarchy. The control you are
    >> looking for may not be a direct child of your pages control collection
    >> but a
    >> child control of the form control in the page. If the control you are
    >> looking for is further nested within another control, then its up to you
    >> to
    >> dig further down the hierarchy and find it.
    >> Currently the code is not executing because you are searching in the
    >> wrong
    >> place where the control is not contained.
    >>
    >> follow the tutorial here :
    >> http://msdn2.microsoft.com/en-us/library/yt340bh4.aspx
    >>
    >> Alessandro Zifiglio
    >> http://www.AsyncUI.net
    >> "George Leithead" <> ha scritto nel messaggio
    >> news:...
    >> > Hi,
    >> >
    >> > I have the following in an ASPX page, that is an event listener, that
    >> > works fine.
    >> >
    >> > BasicSearch1.SearchActivated += new
    >> > myWebControlLibrary.BasicSearch.SearchEventHandler(SearchResults1.PerformSearch);
    >> >
    >> > However, I would like to change the ASPX page to not assume the name of
    >> > the control, but instead listen to the event programmatically. I tried
    >> > the below, which does compile and resolve correctly, however, it does
    >> > not appear to be 'listening' to the event and does not get triggered!
    >> >
    >> > foreach (Control ctrl in Page.Controls)
    >> > {
    >> > if (ctrl.GetType() ==
    >> > typeof(myWebControlLibrary.BasicSearch))
    >> > {
    >> > // This SHOULD have worked! But it doesnt appear to
    >> > indicate that it is listening!
    >> > ((myWebControlLibrary.BasicSearch)ctrl).SearchActivated += new
    >> > WebControlLibrary.BasicSearch.SearchEventHandler(SearchResults1.PerformSearch);
    >> > }
    >> > }
    >> >

    >
     
    Alessandro Zifiglio, Jul 12, 2006
    #4
    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. Poul Møller Hansen

    Listening for files in a directory

    Poul Møller Hansen, Jan 13, 2005, in forum: Perl
    Replies:
    2
    Views:
    448
    Joe Smith
    Jan 16, 2005
  2. Bob Garbados
    Replies:
    5
    Views:
    479
    Bob Garbados
    Dec 22, 2004
  3. kai
    Replies:
    6
    Views:
    610
    Steven Cheng[MSFT]
    Nov 28, 2005
  4. Mombiatch

    Determining which handlers are listening for an event

    Mombiatch, Jun 18, 2004, in forum: ASP .Net Web Controls
    Replies:
    1
    Views:
    120
    DalePres
    Jun 19, 2004
  5. carlos
    Replies:
    1
    Views:
    156
    Paul Wilkins
    Feb 16, 2008
Loading...

Share This Page