(ASP.NET) Control objects?

G

Guest

When programming for a windows application, i can create a Control object,
and then use this object to iterate through a window form's controls. For
example:

foreach(Control ctr in this.Controls)
{
// do something...
}

However, i am unable to create a Control object in ASP.NET. I am forced to
create specific objects for each scenario, for example, a TextBox control,
then a Label control, then a PlaceHolder control.

Is this simply not possible to create an all encompassing Control object w/
ASP.NET? And, if so, is there a "work around", other than creating a specific
control and routine for each scenario?

Thanks,
 
G

Guest

You can use the System.Web.UI.Control. To iterate through all the controls
you could use the following code:

private void Button1_Click(object sender, System.EventArgs e)
{
ProcessControls(this.Page);
}

private void ProcessControls(Control c)
{
if (c.HasControls())
{
foreach(Control c2 in c.Controls)
{
// DO Something with control
c2.Visible=false;
ProcessControls(c2);
}
}
}
 
D

Dan Bass

Charlie,

I think you're missing the point of OOP...

the controls TextBox, Label, Button are all Controls. That is to say they
are objects that have been derived from the Control object.

In your windows app you can get a Control base object reference to a control
through a collection of Controls, this same principle applies to ASP.Net

I'd read up on this more on the net... google on "object oriented
(programming OR design) basics c#"
I found this straight away...
http://en.wikipedia.org/wiki/Object-oriented_programming

Thanks

Dan.
 
G

Guest

Thanks Dan. i'm by no means an expert on this, but i don't think i'm not
mssing the boat on OOP by that much. i *was* looking for help on how to
create this control object - turns out the problem is was using the
"System.Web.UI.WebControls" vs "System.Web.UI.Controls". Thanks again.
 
I

Ignacio Machin \( .NET/ C# MVP \)

Hi,

You can do the same thing using Page.Controls

What is the problem with that?

Cheers,
 
G

Guest

nothing wrong w/ this... especially if you know about it. Coming from Compact
Framework & Smart Device development... there's a different kind of learning
curve w/ ASP.NET. Thanks Ignacio.
 

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