[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

Loop through all controls in a user control

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

Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

Question has a verified solution.

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

Sometimes in DotNetNuke module development you want to swap controls within the same module definition.  In doing this DNN (somewhat annoyingly) swaps the Skin and Container definitions to the default admin selections.  To get around this you need t…
Introduction This article shows how to use the open source plupload control to upload multiple images. The images are resized on the client side before uploading and the upload is done in chunks. Background I had to provide a way for user…
this video summaries big data hadoop online training demo (http://onlineitguru.com/big-data-hadoop-online-training-placement.html) , and covers basics in big data hadoop .
With just a little bit of  SQL and VBA, many doors open to cool things like synchronize a list box to display data relevant to other information on a form.  If you have never written code or looked at an SQL statement before, no problem! ...  give i…
Suggested Courses
Course of the Month17 days, 14 hours left to enroll

829 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