Calling Parent method from UserControl.

Last post 07-08-2009 5:18 AM by Henxon. 7 replies.

Sort Posts:

  • Calling Parent method from UserControl.

    03-19-2007, 10:43 AM

     

    Dear Team

     i am unsing ASP.NET2.0, C#

    i have a usercontrol, aspx page inherted from the MasterPage

    i want the user control to call a method from this aspx page.

    please advise what to do.

    thank you.

  • Re: Calling Parent method from UserControl.

    03-19-2007, 11:56 AM
    • Star
      10,669 point Star
    • ps2goat
    • Member since 11-17-2006, 10:43 PM
    • Posts 1,945

    Why don't you give your user control an event, then the page can call the method when that event is raised?

     

    Or, you could put the method in a separate code file in the app_code folder, and both controls can reference the method from the separate file.

    ---------------------------------------
    MCP - Web Based Client Development .NET 2.0
  • Re: Calling Parent method from UserControl.

    03-19-2007, 12:21 PM

    thank yuo

    i want to ask you a question

    can i make a new method on the app_code "x" and then call X method from the user control and X call the method i want in the aspx page can i do that and how please.

    i want you to know that i am using AJAX.

     

    thank you

  • Re: Calling Parent method from UserControl.

    03-19-2007, 1:22 PM

    please i need your help.

     

    it is very imp.

  • Re: Calling Parent method from UserControl.

    03-20-2007, 10:07 AM
    • Star
      10,669 point Star
    • ps2goat
    • Member since 11-17-2006, 10:43 PM
    • Posts 1,945

    Not easily.  Using an event would be the easiest way.  If you want to do it [similar to] your way, you could add a property to the user control that accepts a delegate. Your main app could create the delegate and pass it to the user control, and the control could execute it at any time.

    FYI, a delegate is like a pointer to a function, so it can allow you to pass the location of the function you want to run to other code.

     

    I HIGHLY recommend using the event or separate method solutions as opposed to this, as it is easier to remember how to implement across numerous pages and it's easier to remember what you did 3 months into the future.

    ---------------------------------------
    MCP - Web Based Client Development .NET 2.0
  • Re: Calling Parent method from UserControl.

    03-20-2007, 11:44 AM
    • Participant
      1,048 point Participant
    • Jonathan Davlin
    • Member since 09-19-2006, 2:13 PM
    • Reston, Va.
    • Posts 234
    ps2goat:

     If you want to do it [similar to] your way, you could add a property to the user control that accepts a delegate. Your main app could create the delegate and pass it to the user control, and the control could execute it at any time.

    Listen to this man.  Calling it the other way is what I refer to as Object Disoriented Programming.  A contained object shouldn't ever be actively calling the parent, creating a two way dependency.  This was one of the nice things pre .NET.  Circular references jumped in your face as memory leaks.

    Throw an event, or expose a property of a delegate type.

    Does your team use the VSIP toolkit or maybe have the bulk of your app factored down to patterns and generators? Do you need in-house controls that will make your current vendors blush? If so, we need to talk.
  • Re: Calling Parent method from UserControl.

    03-21-2007, 11:03 AM
    Answer
    • Contributor
      4,580 point Contributor
    • stmarti
    • Member since 06-06-2006, 12:20 PM
    • Posts 972
    Ramzy N.Ebeid:

    can i make a new method on the app_code "x" and then call X method from the user control and X call the method i want in the aspx page can i do that and how please.

    You can... For example:

    You have an aspx page (test.aspx) and a usercontrol inside it (usercontrol.ascx). And you want to call a method of the aspx page from the user control:

    - Declare your  method as public in  the test.aspx's codebehind  / public void JustTest( ) { } /

    - Now you could call JustTest from anywhere in the usercontrol.ascx codebehind in the following form:

    ( ( test )this.Page ).JustTest( );

    - So the trick is that first you need the usercontrol's page object to cast to the parent page's exact type.

    / If you do it right you get of course intellisense also for ( ( test )this.Page ) Smile



     

  • Re: Calling Parent method from UserControl.

    07-08-2009, 5:18 AM
    • Member
      118 point Member
    • Henxon
    • Member since 06-16-2008, 9:42 AM
    • Posts 109

    If u do it like the last poster said, then you'll bind your WebUserControl to that specific aspx-file and a big part of the reason why to use a WUC will be lost. If you just can use one speicific parent page (class) then you might as well implement the things that the WUC holds directly in to the aspx-page.

    Delegate is the way to go - and it's not always a bad idea to have the WUC depending on certain functions in the parent page - this will create a scenario similar to an interface to a class (e.g., certain functions must exist)

    Regards

    Henxon

Page 1 of 1 (8 items)