?
Solved

Loop through all controls in a user control

Posted on 2011-09-19
3
Medium Priority
?
385 Views
Last Modified: 2012-05-12
How do I properly recurse all of the controls in a User Control (.ascx)?

here is my attempt:

  private Control FindNestedControl(ControlCollection c, string typeasstring, bool value)
        {
            Control tempControl = null;

            foreach (Control cc in c)
            {
                string contype = cc.GetType().ToString();
                if (contype == typeasstring)
                {
                    ((System.Web.UI.WebControls.TextBox)cc).BorderWidth = 0;
                    ((System.Web.UI.WebControls.TextBox)cc).ReadOnly = value;
                    ((System.Web.UI.WebControls.TextBox)cc).Enabled = !value;
                    tempControl = FindNestedControl(cc.Controls, typeasstring, value);
                }
            }

            return tempControl;
            
        }

Open in new window





My intent is to loop through all the controls and make the textbox controls read only.
0
Comment
Question by:Tom Knowlton
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
3 Comments
 
LVL 16

Assisted Solution

by:Easwaran Paramasivam
Easwaran Paramasivam earned 100 total points
ID: 36564761
http://steveorr.net/faq/controltreerecursion.aspx
http://odetocode.com/articles/116.aspx
http://forums.asp.net/t/1219331.aspx/1


Refer below snippet. Please correct the error if any while using the below code in your application.

MyUserControl u = Page.FindControl("MyUserControl1") as MyUserControl;

foreach (Control ctrl in u.Controls)

     {

          if (ctrl is TextBox)

          {

              ((TextBox)(ctrl)).Enabled = false;

          }

         
     }



0
 
LVL 16

Accepted Solution

by:
disrupt earned 1900 total points
ID: 36564920
0
 
LVL 5

Author Closing Comment

by:Tom Knowlton
ID: 36567140
thx
0

Featured Post

Enroll in August's Course of the Month

August's CompTIA IT Fundamentals course includes 19 hours of basic computer principle modules and prepares you for the certification exam. It's free for Premium Members, Team Accounts, and Qualified Experts!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In this Article, I will provide a few tips in problem and solution manner. Opening an ASPX page in Visual studio 2003 is very slow. To make it fast, please do follow below steps:   Open the Solution/Project. Right click the ASPX file to b…
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
If you’ve ever visited a web page and noticed a cool font that you really liked the look of, but couldn’t figure out which font it was so that you could use it for your own work, then this video is for you! In this Micro Tutorial, you'll learn yo…
In this video, Percona Director of Solution Engineering Jon Tobin discusses the function and features of Percona Server for MongoDB. How Percona can help Percona can help you determine if Percona Server for MongoDB is the right solution for …
Suggested Courses

765 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question