Re: please can someone explain the following code.

Discussion in 'ASP .Net' started by suzy, Jul 30, 2003.

  1. suzy

    suzy Guest

    Hi Christian,

    Thanks for the reply, but this is what I don't understand..... (by the way
    there was a typo in my last message: "dpPageInit" should be "doPageInit").

    I am new to inheritance, but I understand that if I create a classA that
    inherits from classB then classA will expose properties/methods of classB.
    The things I don't understand are code like:

    a) override
    b) +=
    c) this.Init += new System.EventHandler


    I am trying to create a page template (ie: a template page that contains a
    header and footer), and then I want each of my pages to inherit the template
    page so all I have to do is add the main body of the ASP page (the header
    and footer will automatically be rendered by the template page).

    I have downloaded the code from the following place
    http://www.aspnetui.com/templates/ but I cant get it to work when I try it
    myself. On top of that I think they have made the example more complex than
    it needs to be.

    All I want is a template that looks just like their red example. If someone
    could post a simplified version of it it would be great.


    "Christian" <> wrote in message
    news:%...
    > Hi Suzy
    > It seems to me that you are in an inherited class, you are overriding
    > virtual method OnInit in order to subscribe dbPageInit to be called in the
    > Init Event, dbPageLoad to be called In the Load Event and calling OnInit
    > method on base class ( that inherited from )...
    >
    > What else aren't you understanding?
    > In case give details..
    > Christian.
    >
    > "suzy" <> ha scritto nel messaggio
    > news:%23%...
    > > please can someone explain the following code to a newbie... thanks
    > >
    > > override protected void OnInit(EventArgs e)
    > > {
    > >
    > > this.Init += new System.EventHandler(dpPageInit);
    > > this.Load += new System.EventHandler(doPageLoad);
    > > base.OnInit(e);
    > > }
    > >
    > >

    >
    >
    suzy, Jul 30, 2003
    #1
    1. Advertising

  2. suzy

    Christian Guest

    a) override:
    If you have:
    Class MyClass
    {
    ...
    protected virtual void MyMethod()
    {
    // Some instructions
    ...
    }
    }
    MyMethod is the first implementation of the method.
    You could have your ownn MyMethod in your class:
    Class MySecondClass : Myclass
    {
    protected override void MyMethod()
    {
    // other instructions
    ...
    }
    }
    You can override only virtual methods.

    b) += :
    add something to.... for example:

    c) this.Init += new System.EventHandler( doPageInit ):
    add doPageInit method to delegate list called by Init event.
    ( Is Init an event ? )
    When Init event is fired your class iterates through delegate: (
    System.EventHandler is our delegate )
    and call all methods subscribed
    you subscribe your own method ( that must have the signature given by
    delegate as return type, parameters, etc ) by writng
    ... += new System.EventHandler( doPageInit ):

    Do you know what delegates are?

    "suzy" <> ha scritto nel messaggio
    news:...
    > Hi Christian,
    >
    > Thanks for the reply, but this is what I don't understand..... (by the way
    > there was a typo in my last message: "dpPageInit" should be "doPageInit").
    >
    > I am new to inheritance, but I understand that if I create a classA that
    > inherits from classB then classA will expose properties/methods of classB.
    > The things I don't understand are code like:
    >
    > a) override
    > b) +=
    > c) this.Init += new System.EventHandler
    >
    >
    > I am trying to create a page template (ie: a template page that contains a
    > header and footer), and then I want each of my pages to inherit the

    template
    > page so all I have to do is add the main body of the ASP page (the header
    > and footer will automatically be rendered by the template page).
    >
    > I have downloaded the code from the following place
    > http://www.aspnetui.com/templates/ but I cant get it to work when I try it
    > myself. On top of that I think they have made the example more complex

    than
    > it needs to be.
    >
    > All I want is a template that looks just like their red example. If

    someone
    > could post a simplified version of it it would be great.



    > > "suzy" <> ha scritto nel messaggio
    > > news:%23%...
    > > > please can someone explain the following code to a newbie... thanks
    > > >
    > > > override protected void OnInit(EventArgs e)
    > > > {
    > > >
    > > > this.Init += new System.EventHandler(dpPageInit);
    > > > this.Load += new System.EventHandler(doPageLoad);
    > > > base.OnInit(e);
    > > > }
    > > >
    > > >

    > >
    > >

    >
    >
    Christian, Jul 30, 2003
    #2
    1. Advertising

  3. suzy

    Christian Guest


    > Thanks for your reply. No I don't know what delegates are. :-(


    should take a look..
    >
    > Also, in your example of MyClass/MyMethod in which order will the code be
    > run?

    MyClass.MyMethod is 1 implementation
    MySecondClass.MyMethod is another 1
    they are not tied
    public Class MyClass
    {
    ...
    protected virtual void MyMethod()
    {
    // Some instructions
    MessageBox.Show( "1" );
    }
    protected virtual void AnotherMethod()
    {
    MessageBox.Show( "Hello" );
    }
    }
    MyMethod is the first implementation of the method.
    You could have your ownn MyMethod in your class:
    public Class MySecondClass : Myclass
    {
    protected override void MyMethod()
    {
    // other instructions
    MessageBox.Show( "2" );
    ...
    }
    protectd override AnotherMethod()
    {
    base.AnotherMethod();
    MessageBox.Show( "Suzy" );
    }
    }
    MyClass cl = new MyClass();
    cl.MyMethod(); // displays 1
    cl.AnotherMethod(); // displays Hello
    MySecondClass cl2 = new MySecondClass();
    cl2.MyMethod(); // displays 2
    cl2.AnotherMethod(); // displays Hello and then Suzy

    talking about delegates and events

    this.Load += new System.EventHandler(doPageLoad);
    this.Load += new System.EventHandler(doPageAnotherLoad);
    this.Load += new System.EventHandler(doPageAnotherOneLoad);

    On Load Fired will be executed
    doPageLoad(), doPageAnotherLoad(), doPageAnotherOneLoad(), i think in order
    of submission

    >
    > Someone said the following code would create a template if placed in a

    page
    > template class:

    Sorry, i'm not the proper person for help you with asp... never worked :-(
    Repost for detailed and specific helps.

    > protected override void OnInit (EventArgs args)
    > {
    > this.Controls.AddAt(0, LoadControl("path to header.ascx " );
    > base.OnInit(e);
    > this.Controls.Add(LoadControl("path to footer.ascx"));
    > }
    >
    > And the code in my page that inherited the above contained the

    following...:
    >
    > override protected void OnInit(EventArgs e)
    >
    > {
    >
    > InitializeComponent();
    >
    > base.OnInit(e);
    >
    > }
    >
    > private void Page_Load(object sender, System.EventArgs e)
    >
    > {
    >
    > placeholder.Controls.Add (grdDataGrid);
    >
    > }
    >
    > They said this should place my placeholder/datagrid between a

    header/footer.
    >
    Christian, Jul 30, 2003
    #3
  4. suzy

    suzy Guest

    Thanks, that explains a lot! Much appreciated!

    I think I am right in saying that if I inherited a template class containing
    the code below in it, then it won't place a header/footer around my ASP code
    (of the page that inherits the template class).

    protected override void OnInit (EventArgs args)
    {
    this.Controls.AddAt(0, LoadControl("path to header.ascx" );
    base.OnInit(e);
    this.Controls.Add(LoadControl("path to footer.ascx"));
    }


    "Tom" <> wrote in message
    news:...
    > Hi Susy,
    > A delegate (or an event) is an kind of pointer to functions. The

    difference
    > between an event and an ordinary pointer is that inside of the event is a
    > list of pointers to functions. If you "call" the event it's called

    raising
    > the event and every function that has been added to the event is called in
    > order.
    >
    > The way this is typicaly used in C# is if you have a class in which things
    > can occur that might be of interest to others, you can declare a public
    > event (like "event EventHandler Load;") Any other object that is

    interested
    > in (i.e wants to be notified of) this occurance (Load) registers this
    > interest by adding a pointer to a function in the event. So when you see
    > the code:
    >
    > this.Load += new System.EventHandler(doPageLoad);
    >
    > you are looking at an expression of interest in the Load event. When it
    > occurs, the doPageLoad() function will be called (along with any other
    > functions that have been registered with the Load event using the +=

    syntax.
    > The odd thing about this code is that it it registering with an event on

    the
    > same object as the current instance. There are better (or at least more
    > natural) ways of accomplishing this. First, if (as is likely) the Load
    > event is declared in a superclass (a class from which this current class

    was
    > derived - directly or indirectly) you would typically override a protected
    > OnLoad() method to obtain the notification of Load. Second, if the event

    is
    > declared in the same object as the += code, you'd typically just call a
    > function directly instead of raising an event and receiving notification

    for
    > it.
    >
    > Tom
    >
    > "suzy" <> wrote in message
    > news:%...
    > > great, i am starting to understand more:
    > >
    > > i thought it worked the way you say it worked, that's why i didn't
    > > understand the theory behind he code that was submitted to me regarding

    > the
    > > page hearder/footer (shown below):
    > >
    > > > protected override void OnInit (EventArgs args)
    > > > {
    > > > this.Controls.AddAt(0, LoadControl("path to header.ascx " );
    > > > base.OnInit(e);
    > > > this.Controls.Add(LoadControl("path to footer.ascx"));
    > > > }

    > >
    > > if the above code is in my base/template class (A), i can't see how i

    can
    > > write code in a class that inherits from it (B), so that my code from B

    > gets
    > > inserted between the header/footer.
    > >
    > > i know you said you haven't done asp.net, but doesn't the theory of it

    > sound
    > > wrong to you?
    > >
    > > "Christian" <> wrote in message
    > > news:...
    > > >
    > > > > Thanks for your reply. No I don't know what delegates are. :-(
    > > >
    > > > should take a look..
    > > > >
    > > > > Also, in your example of MyClass/MyMethod in which order will the

    code
    > > be
    > > > > run?
    > > > MyClass.MyMethod is 1 implementation
    > > > MySecondClass.MyMethod is another 1
    > > > they are not tied
    > > > public Class MyClass
    > > > {
    > > > ...
    > > > protected virtual void MyMethod()
    > > > {
    > > > // Some instructions
    > > > MessageBox.Show( "1" );
    > > > }
    > > > protected virtual void AnotherMethod()
    > > > {
    > > > MessageBox.Show( "Hello" );
    > > > }
    > > > }
    > > > MyMethod is the first implementation of the method.
    > > > You could have your ownn MyMethod in your class:
    > > > public Class MySecondClass : Myclass
    > > > {
    > > > protected override void MyMethod()
    > > > {
    > > > // other instructions
    > > > MessageBox.Show( "2" );
    > > > ...
    > > > }
    > > > protectd override AnotherMethod()
    > > > {
    > > > base.AnotherMethod();
    > > > MessageBox.Show( "Suzy" );
    > > > }
    > > > }
    > > > MyClass cl = new MyClass();
    > > > cl.MyMethod(); // displays 1
    > > > cl.AnotherMethod(); // displays Hello
    > > > MySecondClass cl2 = new MySecondClass();
    > > > cl2.MyMethod(); // displays 2
    > > > cl2.AnotherMethod(); // displays Hello and then Suzy
    > > >
    > > > talking about delegates and events
    > > >
    > > > this.Load += new System.EventHandler(doPageLoad);
    > > > this.Load += new System.EventHandler(doPageAnotherLoad);
    > > > this.Load += new System.EventHandler(doPageAnotherOneLoad);
    > > >
    > > > On Load Fired will be executed
    > > > doPageLoad(), doPageAnotherLoad(), doPageAnotherOneLoad(), i think in

    > > order
    > > > of submission
    > > >
    > > > >
    > > > > Someone said the following code would create a template if placed in

    a
    > > > page
    > > > > template class:
    > > > Sorry, i'm not the proper person for help you with asp... never worked

    > :-(
    > > > Repost for detailed and specific helps.
    > > >
    > > > > protected override void OnInit (EventArgs args)
    > > > > {
    > > > > this.Controls.AddAt(0, LoadControl("path to header.ascx " );
    > > > > base.OnInit(e);
    > > > > this.Controls.Add(LoadControl("path to footer.ascx"));
    > > > > }
    > > > >
    > > > > And the code in my page that inherited the above contained the
    > > > following...:
    > > > >
    > > > > override protected void OnInit(EventArgs e)
    > > > >
    > > > > {
    > > > >
    > > > > InitializeComponent();
    > > > >
    > > > > base.OnInit(e);
    > > > >
    > > > > }
    > > > >
    > > > > private void Page_Load(object sender, System.EventArgs e)
    > > > >
    > > > > {
    > > > >
    > > > > placeholder.Controls.Add (grdDataGrid);
    > > > >
    > > > > }
    > > > >
    > > > > They said this should place my placeholder/datagrid between a
    > > > header/footer.
    > > > >
    > > >
    > > >

    > >
    > >

    >
    >
    suzy, Jul 31, 2003
    #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. Aarti
    Replies:
    3
    Views:
    263
  2. Soumen banerjee

    Can someone please explain to me this code?

    Soumen banerjee, May 3, 2009, in forum: Python
    Replies:
    3
    Views:
    393
  3. wkevin
    Replies:
    4
    Views:
    566
    Edward A. Falk
    Nov 7, 2011
  4. John Small
    Replies:
    4
    Views:
    97
    John Small
    Feb 2, 2009
  5. Replies:
    6
    Views:
    111
Loading...

Share This Page