How do I inherit controls?

J

justsome_newbie

Sorry for posting another question, but I can't seem to get the hang of
ASP.Net. In windows apps if I want to extend a control I would just
create a class that inherits from that control and do
add/overload/override the necessary properties and methods. This
apparently doesn't work in Asp.Net, or at least I can't get it to.

Take the simple class below:

public class MyTextBox : System.Web.UI.WebControls.TextBox
{
public MyTextBox()
{
}

public String HelloWorldText
{
get
{
return "Hello World";
}
}

public void ChangeText()
{
this.Text = this.HelloWorldText;
}
}

I then added the below to the host page to load the control (since I
couldn't find out how to do it at design time):

protected void Page_Load(object sender, EventArgs e)
{
Page.Controls.Add(new MyTextBox());
}

When run this fails with the error "Control 'ctl02' of type 'MyTextBox'
must be placed inside a form tag with runat=server."

I have no idea what this means (this is still my first ASP.Net app),
could any of you experts offer some guidance as to how I should extend
a control?

Thanks in advance!
 
M

Mark Rae

When run this fails with the error "Control 'ctl02' of type 'MyTextBox'
must be placed inside a form tag with runat=server."

I have no idea what this means (this is still my first ASP.Net app),
could any of you experts offer some guidance as to how I should extend
a control?

I appreciate that you're a newbie, but a quick Google search on the error
message you're getting would have given you the answer straightaway:
http://www.google.co.uk/search?sour...e+placed+inside+a+form+tag+with+runat=server"

In ASP.NET, webcontrols need to be created in a server-side <form> object.
It's a good idea to use an <asp:panel> control for custom controls, as it
gives you an extra bit of control over where they get created, e.g.

<form id="frmDefault" runat="server">
<asp:panel ID="pnlControls" runat="server">

</asp:panel>
</form>

Now, you should be able to do:

pnlControls.Controls.Add(new MyTextBox());

You should really consider getting a beginner's guide to ASP.NET and working
your way through it.
 
J

justsome_newbie

Thanks for the help again, I really appreciate it. I'll try out your
suggestions and the ones on the link you provided.
I appreciate that you're a newbie, but a quick Google search on the error
message you're getting would have given you the answer straightaway:

To redeem myself a little, I did search for things like "inherit
control" and "extend control", but I failed to run the now obvious
search for the error message. Sorry about that, I'll try to learn from
this mistake and spend more time searching before posting in the
future.
You should really consider getting a beginner's guide to ASP.NET and working
your way through it.

Any recommendations? I don't really need help with the language (I'm
well grounded in C#)
I just don't understand the Asp.net programming model and it's
differences with windows apps (all my experience is with windows apps).
Besides books, are there any good websites that might have this info
available?

Thanks again for your time!
 
M

Mark Rae

To redeem myself a little, I did search for things like "inherit
control" and "extend control", but I failed to run the now obvious
search for the error message. Sorry about that, I'll try to learn from
this mistake and spend more time searching before posting in the
future.

No problem. Generally speaking, when I come across a problem, I look for
help in the following places (in order):

1) Visual Studio.NET's own help system - if it's found an error, especially
while debugging, chances are it knows *exactly* what has caused it...

2) http://msdn.microsoft.com/library

3) http://www.google.com

4) These newsgroups
Any recommendations? I don't really need help with the language (I'm
well grounded in C#)
I just don't understand the Asp.net programming model and it's
differences with windows apps (all my experience is with windows apps).

http://www.amazon.com/ASP-NET-All-R..._bbs_sr_1/105-7348212-5254862?ie=UTF8&s=books
http://www.amazon.com/ASP-NET-Every..._bbs_sr_3/105-7348212-5254862?ie=UTF8&s=books

The "Dummies" series of books are uniformly excellent, though I really hate
the titles! Not knowing about one particular subject does not make a person
dumb per se - I don't know how to fly an aeroplane, but I don't consider
that that makes me "dumb" in any way... Why they couldn't have used the word
"Beginners" instead of "Dummies" I'll never know... :)
Besides books, are there any good websites that might have this info
available?

Again, Google is your friend...:)
http://www.google.co.uk/search?sour...GGLG,GGLG:2006-28,GGLG:en&q=asp.net+tutorials
 

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,743
Messages
2,569,478
Members
44,898
Latest member
BlairH7607

Latest Threads

Top