CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 3 of 3
  1. #1
    Join Date
    Sep 2001
    Posts
    16

    Enumerate all controls in ASP.NET Form

    Hi,

    I need to enumerate all controls in a ASP.NET Form and make them read-only. In old VB6 I do it by following code:

    Dim t As Control
    For Each t In Form1.Controls
    If TypeOf t Is TextBox Then
    t.Locked = True
    End If
    Next

    But how to do it in ASP.NET?



  2. #2
    Join Date
    Mar 2002
    Location
    Alpharetta, GA
    Posts
    51

    Re: Enumerate all controls in ASP.NET Form

    The Page object available from within your web form code behind has a controls collection. I'm not sure if you can set the controls to read only but you can disable them. You'll need to cast to WebControl because enumerating controls returns type Control.

    You can do the cast in a try catch block or us the as keyword:



    int total = Page.Controls.Count;

    for (int i = 0; i<total; i++)
    {
    WebControl c = Page.Controls[i] as WebControl;

    if (c!=null)
    {
    // do your work here
    }

    }



    or something like that


  3. #3
    Join Date
    Mar 2002
    Posts
    5
    here's another way which I think is better:

    foreach (WebControl ctl in Page.Controls)
    {
    TextBox tBox = (TextBox) ctl;
    if (tBox != null)
    {
    \\ whatever you want to do
    }
    }

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured