can webform inherit controls from another web form

D

David

I have trying to have a webform inherit controls from another form and can't
get it to work

Say I have a form that saves the person's demographic info.

****one.aspx****

//I have an object to save the person's name in code behind

protected void SavePersonInfo(Person p)

{

p.First = txtFirstName.Text;

p.Last = txtLastName.Text;

p.Save();

}

******************

Then I have another customer that wants the same form, but would like the
person's gender and age in additioon to the first and last names.

Ideally, I should be able to have a form that inherits the form above and do

****derived.aspx**************

protected new void SavePersonInfo(Person p)

{

p.Age = txtAge.Text;

p.Gender = cmbGender.SelectedValue;

base.SavePersonInfo();

}

********************


I just don't want to have to repeat the code behind for first and last names
for the second customer's form. So ideally the derived form inherits
txtFirstName and txtLastName.

But the problem is VS.NET won't let you. When I draw txtFirstName, and
txtLastName, VS.NET keeps declaring them as class level controls in the
derived class. I comment them out and they keep coming back. After I comment
them out, I can compile and run and the code expects the way I want it to,
but the bam, these two lines keep come back again.

protected TextBox txtFirstName;

protected TextBox txtLastName;
 
M

matt

If you take out the codebehind attribute then when you're editing the
page VS.net won't try to help by adding in the additional fields.
 
D

David

What do you mean? The aspx page needs know where its codebehind is, so how
can you take out that attribute. I am not understanding what you mean by
"take out the codebehind attribute." Thanks in advance..
 
M

matt

The codebehind attribute is only needed by visual studio not by ASP.Net.
For the page to be served correctly you just need to inherit from the
correct class, if you do not set the codebehind attribute then visual
studio will not make changes to the class you inherit from, which should
solve your problem.

for example...

<%# Page language="c#" AutoEventWireup="false"
Inherits="MyNamespace.MyClass" %>

Matt
 
D

David

Thanks. I got it to work, but...

When I want to hide (new) the base form's behavior (say the button event
handler), and the base forms' fields as well as the event handler, I run
into the following problem.

You have to take out base.OnInit(e) in the derived form's OnInit(EventArgs
e). If you don't, then the base form's InitializeComponent executes, and
will throw an Null Exception error since it can't find any of the declared
controls (the derived form hides them with "new"). I just don't know the
ramifications of taking out base.OnInit(e) is and I am not sure if that is
something I should mess with.

It is getting a bit too complicated and is defeating code maintenanbility,
which was my original intent.
 

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

Top