Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Loop through all controls in a user control

Posted on 2011-09-19
3
Medium Priority
?
387 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

Ask an Anonymous Question!

Don't feel intimidated by what you don't know. Ask your question anonymously. It's easy! Learn more and upgrade.

Question has a verified solution.

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

I have developed many web applications with asp & asp.net and to add and use a dropdownlist was always a very simple task, but with the new asp.net, setting the value is a bit tricky and its not similar to the old traditional method. So in this a…
Problem Hi all,    While many today have fast Internet connection, there are many still who do not, or are connecting through devices with a slower connect, so light web pages and fast load times are still popular.    If your ASP.NET page …
In response to a need for security and privacy, and to continue fostering an environment members can turn to for support, solutions, and education, Experts Exchange has created anonymous question capabilities. This new feature is available to our Pr…
We’ve all felt that sense of false security before—locking down external access to a database or component and feeling like we’ve done all we need to do to secure company data. But that feeling is fleeting. Attacks these days can happen in many w…
Suggested Courses

618 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