Inheritance and WebControls

M

Mr Flibble

I've created a baseclass thusly:

public class BasePage : System.Web.UI.Page

and then I have another class Default3 created from this:

public partial class Default3 : BasePage

I've put all my common code in BasePage and so I have my implementation
in one place now for the common features of my website. All good so far.
A webpage with lots of code, all of which inherited. Now I can tailor
each page specifically.

One problem is that I have a WebChart control in my page.

e.g. <Web:ChartControl /> in my .aspx.

I'd like this control to be in all my pages to but you cant inherit the
HTML from the .aspx can you? So what I've done is I've created a method
that sets up the webchart control, and put this in the base class then I
get Default3 to call this method passing the ChartControl of its aspx as
a parameter. So I have this method signature in BasePage;

public void SetupChart(ref ChartControl uxMyChart)

I have a few questions, and I hope I'm explaining what I've done
clearly! One is that although my webpage calls this SetupChart, the
chart which correctly creates a PNG creates the file *BUT* the src= part
of the rendered HTML is blank. So this method is not working for some
reason, when the code to setup the chart is in BasePage and not in
Default3. This has to be some oversight on my part, that hopefully
someone can explain?!

On a probably more important note, is there a better way of sharing the
code to setup the chart so that I dont have to call some method?

Also now that I have the relationship Web.UI.Page -> BasePage ->
Default3, if I want to put something in the Page_Load method in Default3
am I still able to? Am I able to have an implementation of Page_Load
both in the BasePage AND in Default3?

Hope I've explained my situation well and its not as clear as mud!

Flibble
 
F

Flinky Wisty Pomm

Your question might be better answered if you post your code. I can't
see any real problems with what you're attempting. Re: Page_Load, yes
you can.

BasePage:
protected void Page_Load(object sender, EventArgs e)
{
DoBaseThings();
}

Default3:

protected override void Page_Load(object sender, EventArgs e)
{
DoPageSpecificThings();
base.Page_Load(sender, e);
}
 
M

Mr Flibble

* Flinky Wisty Pomm said:
Your question might be better answered if you post your code. I can't
see any real problems with what you're attempting. Re: Page_Load, yes
you can.

BasePage:
protected void Page_Load(object sender, EventArgs e)
{
DoBaseThings();
}

Default3:

protected override void Page_Load(object sender, EventArgs e)
{
DoPageSpecificThings();
base.Page_Load(sender, e);
}

I've tried this already but I get

Error 1 'Default3.Page_Load(object, System.EventArgs)': cannot override
inherited member 'BasePage.Page_Load(object, System.EventArgs)' because
it is not marked virtual, abstract, or override
 
F

Flinky Wisty Pomm

I've tried this already but I get

Error 1 'Default3.Page_Load(object, System.EventArgs)': cannot override
inherited member 'BasePage.Page_Load(object, System.EventArgs)' because
it is not marked virtual, abstract, or override

The easy answer to that is: Mark is as virtual:

BasePage:

protected virtual void SomeMethod()

Default3:
protected override void SomeMethod()
 
M

Mr Flibble

* Flinky Wisty Pomm said:
The easy answer to that is: Mark is as virtual:

BasePage:

protected virtual void SomeMethod()

Default3:
protected override void SomeMethod()

Ok that worked a treat. You asked for some sample code to explain the
issue of the webcontrol. Well here it goes!

In my Page_Load in Default3 I make a call to the inherited method like this:

protected override void Page_Load(object sender, EventArgs e)
{
CreateChart(ref uxChartPrices);
base.Page_Load(sender, e);
}

I do this because I need to modify a webcontrol and my BasePage doesn't
contain a webcontrol, so I pass one in.

This now works a treat, my WebChart control now produces the PNG and
references it.

But before when I wasn't using Page_Load to setup the chart but rather
had <%CreateChart(ref uxChartPrices);%> within my page (just before the
closing top level </html> tag) it didn't work. N.B. In CreateChart I
just have some code to setup and configure the WebChart control I'm
using. This code is tried and tested and works a treat so it's nothing
within this routine.

The fact that it now works means that the WebChart control must need
setting up before the <Web:ChartControl > is read within the .aspx?

Maybe even putting the original <%CreateChart(ref uxChartPrices);%> back
in the aspx file BUT positioning it at the top (near the opening <html)
would have given me the same result as moving CreateChart to Page_Load?

Certainly seems to a be a case of when the CreateChart gets executed,
that gives me either a success or a failure of the chart being correctly
displayed in the page.

How do people that use WebControls manage to setup these controls via a
common routine , using ASP.NET ? As I have done or is there a better way?
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,012
Latest member
RoxanneDzm

Latest Threads

Top