By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
459,996 Members | 1,193 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 459,996 IT Pros & Developers. It's quick & easy.

can webform inherit controls from another web form

P: n/a
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;
Nov 18 '05 #1
Share this Question
Share on Google+
4 Replies


P: n/a
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.

"David" <sp************@spammers.go.home.com> wrote in message
news:1V***************@twister.socal.rr.com...
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;

Nov 18 '05 #2

P: n/a
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..

"matt" <gr************@hitscricket.com> wrote in message
news:7Z*********************@stones.force9.net...
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.

Nov 18 '05 #3

P: n/a
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

"David" <sp************@spammers.go.home.com> wrote in message
news:ft**************@twister.socal.rr.com...
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..

"matt" <gr************@hitscricket.com> wrote in message
news:7Z*********************@stones.force9.net...
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.


Nov 18 '05 #4

P: n/a
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.

"matt" <gr************@hitscricket.com> wrote in message
news:pM*********************@wards.force9.net...
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

"David" <sp************@spammers.go.home.com> wrote in message
news:ft**************@twister.socal.rr.com...
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..

"matt" <gr************@hitscricket.com> wrote in message
news:7Z*********************@stones.force9.net...
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.



Nov 18 '05 #5

This discussion thread is closed

Replies have been disabled for this discussion.