Normally when I use this code, I place the code inside of a container control such as a panel so
foreach(Control ctrl in panel1.Controls)
{
//code
}
However, the reason you are having your above problem is because the Form, in and of itself, is a container control so you need to do something like this:
foreach(Control ctrl in Page.Controls)
{
if(!ctrl is TextBox)
{
if(ctrl.HasControls)
{
ReplaceText(ctrl);
}
}
else
{
TextBox t = (TextBox)ctrl;
t.Text = t.Text.Replace("\"", "");
}
}
private void ReplaceText(Control crl)
{
foreach(Control ctrl in crl.Controls)
{
if(!ctrl is TextBox)
{
if(ctrl.HasControls)
{
ReplaceText(ctrl);
}
}
else
{
TextBox t = (TextBox)ctrl;
t.Text = t.Text.Replace("\"", "");
}
}
So, with all of that said, you are now at a point where it is more effecient to just deal with the 10 lines of code that you originally used to replace your quotes.
To address Gonzalo's point, he is right, this is going to change every value of every textbox on a given page however, given your original post, you did not say that there were textboxs on the page that this behavior should not be persisted to.
In all reality the only time I would ever use a recursive call such as above is if I am working with a page that has many dynamic controls (and control containers for that matter) and the id's of said controls are unknown to me at runtime.
hth.
================================================== =========
Read this if you want to know how to get a correct reply for your question:
http://www.catb.org/~esr/faqs/smart-questions.html
================================================== =========
Technical Editor for:
Professional Search Engine Optimization with ASP.NET
Professional IIS 7 and ASP.NET Integrated Programming
================================================== =========