Solved

Loop through all controls in a user control

Posted on 2011-09-19
3
358 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:knowlton
3 Comments
 
LVL 16

Assisted Solution

by:Easwaran Paramasivam
Easwaran Paramasivam earned 25 total points
Comment Utility
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 475 total points
Comment Utility
0
 
LVL 5

Author Closing Comment

by:knowlton
Comment Utility
thx
0

Featured Post

Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

A quick way to get a menu to work on our website, is using the Menu control and assign it to a web.sitemap using SiteMapDataSource. Example of web.sitemap file: (CODE) Sample code to add to the page menu: (CODE) Running the application, we wi…
User art_snob (http://www.experts-exchange.com/M_6114203.html) encountered strange behavior of Android Web browser on his Mobile Web site. It took a while to find the true cause. It happens so, that the Android Web browser (at least up to OS ver. 2.…
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…
You have products, that come in variants and want to set different prices for them? Watch this micro tutorial that describes how to configure prices for Magento super attributes. Assigning simple products to configurable: We assigned simple products…

772 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

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now