Finding Controls in a Page

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • mayankaswal
    New Member
    • Apr 2007
    • 12

    #1

    Finding Controls in a Page

    I am trying to go through all controls in a page and if its a text box then making it non editable.... how do i go about it...
  • Plater
    Recognized Expert Expert
    • Apr 2007
    • 7872

    #2
    There is like a Page.Controls or Page.Components or something.
    loop through them checking to see if they are of type textbox.
    If they are, mark them as readonly.

    Comment

    • mayankaswal
      New Member
      • Apr 2007
      • 12

      #3
      I am trying to do something like this... but their is no option as where to match the control if its a text box....??

      foreach (Controls control in Page.Controls)
      {
      if control.?? = textbox
      {
      }

      }

      Comment

      • Plater
        Recognized Expert Expert
        • Apr 2007
        • 7872

        #4
        should be something like
        Code:
        foreach (Control c in Page.Controls)
        {
           if (c.GetType()==typeof(TextBox)
           {
              ((TextBox)c).ReadOnly=true;
           }
        }
        Last edited by Plater; May 21 '07, 05:02 PM. Reason: fixed code typeo

        Comment

        • mayankaswal
          New Member
          • Apr 2007
          • 12

          #5
          The system is not giving my any option when I put a . after c.....

          when i try this

          if(c.??? I get nothing.... have you tried this on your system???

          Comment

          • Plater
            Recognized Expert Expert
            • Apr 2007
            • 7872

            #6
            Originally posted by mayankaswal
            The system is not giving my any option when I put a . after c.....

            when i try this

            if(c.??? I get nothing.... have you tried this on your system???
            Did you note this line change?
            Code:
            foreach (Controls c in Page.Controls)

            Comment

            • mayankaswal
              New Member
              • Apr 2007
              • 12

              #7
              When I type in your code

              foreach (Controls c in Page.Controls)
              {
              if (c.GetType() == typeof(TextBox) )
              {
              // No Statements here till now
              }
              }

              It gives me followiing error.....

              ........Control s' is a 'namespace' but is used like a 'type'

              The type or namespace name 'TextBox' could not be found (are you missing a using directive or an assembly reference?)

              Comment

              • Plater
                Recognized Expert Expert
                • Apr 2007
                • 7872

                #8
                Originally posted by mayankaswal
                When I type in your code

                foreach (Controls c in Page.Controls)
                {
                if (c.GetType() == typeof(TextBox) )
                {
                // No Statements here till now
                }
                }

                It gives me followiing error.....

                ........Control s' is a 'namespace' but is used like a 'type'

                The type or namespace name 'TextBox' could not be found (are you missing a using directive or an assembly reference?)
                OOPS! That is my fault!
                Code:
                foreach (Control c in Page.Controls)

                Comment

                • mayankaswal
                  New Member
                  • Apr 2007
                  • 12

                  #9
                  The type or namespace name 'Control' could not be found (are you missing a using directive or an assembly reference?)

                  The type or namespace name 'TextBox' could not be found (are you missing a using directive or an assembly reference?)

                  Maybe it will work... I guess...:)

                  where do I include textbox and contol???

                  I have included
                  using system.text.
                  using system.collecti ons
                  using system

                  Comment

                  • Plater
                    Recognized Expert Expert
                    • Apr 2007
                    • 7872

                    #10
                    Well if you have Page.Controls and don't have Control, you're in bigger trouble then not matching a textbox type.
                    But yeah, go to the aspx code page and look for the include/using statements up top and add them to the file were you are.

                    Comment

                    • mayankaswal
                      New Member
                      • Apr 2007
                      • 12

                      #11
                      hey...thanx ma.... spelling mstake he he... worst thing to do.....

                      got it now... thanx a lot....

                      Comment

                      Working...