ASP.net control tree, recursion...

G

Guest

I am working on an ASP.net application and I have a base class for all my
pages to inherit. In this class I have a Subroutine named ResetControls
which can go through and set certain types of controls to the defaults that I
specify. I chose to place it here so that all of my pages can use it and I
do not have to place the code on everypage. However, I noticed that if I
have a HTML table running as a server control within another table running as
a server control; the controls inside do not go to the default values that I
specify but all others not in tables do. Is there a way that I can
recursively go through the control tree to reset them so that no matter how
many nested tables are on a page they all can reset to their defaults?
 
K

Kevin Yu [MSFT]

Hi

First of all, I would like to confirm my understanding of your issue. From
your description, I understand that when you are going through each control
on a webform to set the default values, the child controls' value were not
set. If there is any misunderstanding, please feel free to let me know.

You're getting this behavior, because ResetControls subroutine, you didn't
set the value for the child controls. Child controls are not listed in the
controls property of a page, so we have to use ResetControls recursively.
Here is an example

When you get a control in the controls collection.

foreach(Control c in control.Controls)
{
ResetControls (c);
}

ResetControls accepts a Control type as argument to set default value
within this control.

HTH.

Kevin Yu
=======
"This posting is provided "AS IS" with no warranties, and confers no
rights."
 
G

Guest

I tried what you gave, but it seems that when I have an HTML table running as
a server control nested within another table running as a server control, the
controls do not reset. is there a way that I can check for nested tables in
this case. The controls are not showing up in the top-most level of the
control tree. If I parse through the table, row, then cell, I can see the
control. Can I recursively go through nested tables to find controls?
 
K

Kevin Yu [MSFT]

Hi,

As far as I can see, there is not a general way to go though all the
controls on a page. We can directly access the controls property for child
controls, but for controls like HTML Table, we have to go into cells for
child controls. In this case, we can only write a switch case for the
exceptional situation for cases like HTML tables.

Kevin Yu
=======
"This posting is provided "AS IS" with no warranties, and confers no
rights."
 

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