Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > ASP .Net > Re: please can someone explain the following code.

Reply
Thread Tools

Re: please can someone explain the following code.

 
 
suzy
Guest
Posts: n/a
 
      07-30-2003
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" <(E-Mail Removed)> wrote in message
news:%(E-Mail Removed)...
> 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" <(E-Mail Removed)> ha scritto nel messaggio
> news:%23%(E-Mail Removed)...
> > 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);
> > }
> >
> >

>
>



 
Reply With Quote
 
 
 
 
Christian
Guest
Posts: n/a
 
      07-30-2003
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" <(E-Mail Removed)> ha scritto nel messaggio
news:(E-Mail Removed)...
> 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" <(E-Mail Removed)> ha scritto nel messaggio
> > news:%23%(E-Mail Removed)...
> > > 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);
> > > }
> > >
> > >

> >
> >

>
>



 
Reply With Quote
 
 
 
 
Christian
Guest
Posts: n/a
 
      07-30-2003

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



 
Reply With Quote
 
suzy
Guest
Posts: n/a
 
      07-31-2003
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" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> 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" <(E-Mail Removed)> wrote in message
> news:%(E-Mail Removed)...
> > 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" <(E-Mail Removed)> wrote in message
> > news:(E-Mail Removed)...
> > >
> > > > 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.
> > > >
> > >
> > >

> >
> >

>
>



 
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
70-292: can someone please verify / explain the answer to this question Tim Moor MCSE 7 12-18-2005 03:30 AM
Can someone please explain this? harry HTML 12 05-26-2005 06:59 PM
Can Someone Please Explain... BluDog ASP .Net 4 10-05-2004 08:24 AM
Can someone please explain 'em' to me? delerious@no.spam.com HTML 4 12-04-2003 02:28 PM
can someone explain this to me please Mike Henley Java 6 09-07-2003 03:19 AM



Advertisments