+ Reply to Thread
Page 1 of 2 1 2 LastLast
Results 1 to 10 of 16

Thread: Collection of Form Controls

  1. #1
    Administrator richyrich is a splendid one to behold richyrich is a splendid one to behold richyrich is a splendid one to behold richyrich is a splendid one to behold richyrich is a splendid one to behold richyrich is a splendid one to behold richyrich is a splendid one to behold richyrich's Avatar
    Join Date
    Mar 2008
    Location
    Somewhere only we know...
    Posts
    3,207
    Blog Entries
    14
    Real Name
    Rich
    Rep Power
    14

    Collection of Form Controls

    Is there an easy way to loop through just all the form based controls; textbox, dropdownlist etc. and disable them, effectively so they are read only?

  2. #2
    The Barnfather jmurrayhead has much to be proud of jmurrayhead has much to be proud of jmurrayhead has much to be proud of jmurrayhead has much to be proud of jmurrayhead has much to be proud of jmurrayhead has much to be proud of jmurrayhead has much to be proud of jmurrayhead has much to be proud of jmurrayhead has much to be proud of jmurrayhead has much to be proud of jmurrayhead's Avatar
    Join Date
    Mar 2008
    Location
    Reston, VA
    Posts
    4,547
    Blog Entries
    9
    Real Name
    Jason
    Rep Power
    22

    I use this to reset my forms. You should be able to modify it to do as you need:
    Code:
    Public Shared Sub ResetForm(ByVal pageControls As ControlCollection)
        For Each contl As Control In pageControls
            Dim contlName As String = contl.GetType().Name
            Select Case contlName
                Case "TextBox"
                    Dim tbSource As TextBox = CType(contl, TextBox)
                    tbSource.Text = String.Empty
                Case "RadioButtonList"
                    Dim rblSource As RadioButtonList = CType(contl, RadioButtonList)
                    tbSource.SelectedIndex = -1
                Case "DropDownList"
                    Dim ddlSource As DropDownList = CType(contl, DropDownList)
                    ddlSource.SelectedIndex = -1
                Case "ListBox"
                    Dim lstSource As ListBox = CType(contl, ListBox)
                    lstSource.SelectedIndex = -1            
                Case "CheckBox"
                    Dim chkSource As CheckBox = CType(contl, CheckBox)
                    chkSource.Checked = False
            End Select
     
            ResetForm(contl.Controls)
        Next
    End Sub
    
    To call, do this:
    Code:
    ResetForm(Page.Controls)
    
    OR:
    Code:
    ResetForm(pnlContainer.Controls)
    
    The second example only gets the control collection of the Panel "pnlContainer" and leaves the rest on the page alone.
    jmurrayhead
    If you agree, give me rep.
    If you like it here...throw us a few bones to help support us.


  3. #3
    Administrator richyrich is a splendid one to behold richyrich is a splendid one to behold richyrich is a splendid one to behold richyrich is a splendid one to behold richyrich is a splendid one to behold richyrich is a splendid one to behold richyrich is a splendid one to behold richyrich's Avatar
    Join Date
    Mar 2008
    Location
    Somewhere only we know...
    Posts
    3,207
    Blog Entries
    14
    Real Name
    Rich
    Rep Power
    14

    Thanks J.

    I'd sort of come up with this, so far, but it's not disabling the textboxes.
    Code:
                    foreach (Control ctrl in Form.Controls)
                    {
                        TextBox txtbox;
                        if(ctrl.GetType()==typeof(TextBox))
                        {
                            txtbox = (TextBox)ctrl;
                            txtbox.Enabled = false;
                        }
                    }
    
    I presume it doesn't make any difference that my Master Page contains the form tag etc., does it?

  4. #4
    The Barnfather jmurrayhead has much to be proud of jmurrayhead has much to be proud of jmurrayhead has much to be proud of jmurrayhead has much to be proud of jmurrayhead has much to be proud of jmurrayhead has much to be proud of jmurrayhead has much to be proud of jmurrayhead has much to be proud of jmurrayhead has much to be proud of jmurrayhead has much to be proud of jmurrayhead's Avatar
    Join Date
    Mar 2008
    Location
    Reston, VA
    Posts
    4,547
    Blog Entries
    9
    Real Name
    Jason
    Rep Power
    22

    How are you referencing "Form"? What happens if you replace Form.Controls with Page.Controls?
    jmurrayhead
    If you agree, give me rep.
    If you like it here...throw us a few bones to help support us.


  5. #5
    Administrator richyrich is a splendid one to behold richyrich is a splendid one to behold richyrich is a splendid one to behold richyrich is a splendid one to behold richyrich is a splendid one to behold richyrich is a splendid one to behold richyrich is a splendid one to behold richyrich's Avatar
    Join Date
    Mar 2008
    Location
    Somewhere only we know...
    Posts
    3,207
    Blog Entries
    14
    Real Name
    Rich
    Rep Power
    14

    Same. You can still edit the text in the textboxes.

  6. #6
    Administrator richyrich is a splendid one to behold richyrich is a splendid one to behold richyrich is a splendid one to behold richyrich is a splendid one to behold richyrich is a splendid one to behold richyrich is a splendid one to behold richyrich is a splendid one to behold richyrich's Avatar
    Join Date
    Mar 2008
    Location
    Somewhere only we know...
    Posts
    3,207
    Blog Entries
    14
    Real Name
    Rich
    Rep Power
    14

    OK. I added:-
    Code:
                            txtbox.Text = "This Text";
    
    in the if condition and it's this that's failing. So I never get an instance of
    Code:
    if(ctrl.GetType()==typeof(TextBox))
    
    Strange...

  7. #7
    The Barnfather jmurrayhead has much to be proud of jmurrayhead has much to be proud of jmurrayhead has much to be proud of jmurrayhead has much to be proud of jmurrayhead has much to be proud of jmurrayhead has much to be proud of jmurrayhead has much to be proud of jmurrayhead has much to be proud of jmurrayhead has much to be proud of jmurrayhead has much to be proud of jmurrayhead's Avatar
    Join Date
    Mar 2008
    Location
    Reston, VA
    Posts
    4,547
    Blog Entries
    9
    Real Name
    Jason
    Rep Power
    22

    Quote Originally Posted by richyrich View Post
    Same. You can still edit the text in the textboxes.
    Have you tried my method?
    Code:
    public static void DisableFields(ControlCollection pageControls) 
    { 
        foreach (Control contl in pageControls) { 
            string contlName = contl.GetType().Name; 
            switch (contlName) { 
                case "TextBox": 
                    TextBox tbSource = (TextBox)contl; 
                    tbSource.Enabled = false; 
                    break; 
                case "RadioButtonList": 
                    RadioButtonList rblSource = (RadioButtonList)contl; 
                    tbSource.Enabled = false; 
                    break; 
                case "DropDownList": 
                    DropDownList ddlSource = (DropDownList)contl; 
                    ddlSource.Enabled = false; 
                    break; 
                case "ListBox": 
                    ListBox lstSource = (ListBox)contl; 
                    lstSource.Enabled = false; 
                    break; 
                case "CheckBox": 
                    CheckBox chkSource = (CheckBox)contl; 
                    chkSource.Enabled = false; 
                    break; 
            } 
            
            DisableFields(contl.Controls); 
        } 
    }
    
    jmurrayhead
    If you agree, give me rep.
    If you like it here...throw us a few bones to help support us.


  8. #8
    Administrator richyrich is a splendid one to behold richyrich is a splendid one to behold richyrich is a splendid one to behold richyrich is a splendid one to behold richyrich is a splendid one to behold richyrich is a splendid one to behold richyrich is a splendid one to behold richyrich's Avatar
    Join Date
    Mar 2008
    Location
    Somewhere only we know...
    Posts
    3,207
    Blog Entries
    14
    Real Name
    Rich
    Rep Power
    14

    No, I haven't. I'd kind of got to this point before you posted.

    I think I see the problem though. The Page.Controls and Form.Controls are only returning controls directly in the page or form. My textboxes are in a user page, not in the master page so they are in a ContentPlaceHolder.

    In yours, I think this line would allow for that:-
    Code:
    DisableFields(contl.Controls);
    

  9. #9
    Administrator richyrich is a splendid one to behold richyrich is a splendid one to behold richyrich is a splendid one to behold richyrich is a splendid one to behold richyrich is a splendid one to behold richyrich is a splendid one to behold richyrich is a splendid one to behold richyrich's Avatar
    Join Date
    Mar 2008
    Location
    Somewhere only we know...
    Posts
    3,207
    Blog Entries
    14
    Real Name
    Rich
    Rep Power
    14

    OK. The easiest way would be for me to get the control collection in the <asp:Content> control, but you don't seem to be able to access that in your page. Is that possible?

    I'm using Page.Controls in my .aspx page, not the master page, so why is it returning the controls from the master page and not the .aspx page itself?

  10. #10
    Wolfmaster Wolffy is a splendid one to behold Wolffy is a splendid one to behold Wolffy is a splendid one to behold Wolffy is a splendid one to behold Wolffy is a splendid one to behold Wolffy is a splendid one to behold Wolffy is a splendid one to behold Wolffy is a splendid one to behold Wolffy's Avatar
    Join Date
    Mar 2008
    Location
    Peoria, IL
    Posts
    2,386
    Blog Entries
    5
    Real Name
    Wolff
    Rep Power
    15

    I may be wrong here (it's before coffee for me), but if you place all the controls you need to Enable/Disable inside an asp:Panel control -- you should be able to just disable the Panel and all its child controls will go with it.

    Otherwise, if you want to disable all the disable-able controls then the following loop takes advantage of these Controls inheriting from WebControl. (tho this is untested):
    Code:
    foreach (Control ctl in Form.Controls) 
    {
       WebControl webctl = ctl as WebControl;
       if (webctl == null) continue;
       webctl.Enabled = false;
    }
    
    Wolffy
    .-- ----- ..-. ..-. -.--
    Opinions expressed are my own and do not necessity reflect those of any sane person. Any code provided is intended to be an example and is provided AS IS. Void where prohibited by law. Not valid in California. Your mileage may vary.

+ Reply to Thread
Page 1 of 2 1 2 LastLast

Similar Threads

  1. User Controls
    By todd2006 in forum .NET Development
    Replies: 51
    Last Post: August 14th, 2009, 10:54 AM
  2. '?companyname' not found in the collection.
    By richyrich in forum MySQL
    Replies: 3
    Last Post: June 19th, 2009, 07:07 AM
  3. loop through a collection
    By Shem in forum .NET Development
    Replies: 15
    Last Post: October 17th, 2008, 11:54 AM
  4. trying to reference my user controls properties?
    By Shem in forum .NET Development
    Replies: 3
    Last Post: October 3rd, 2008, 05:06 AM
  5. Accessing controls in the Footer of a datalist
    By richyrich in forum .NET Development
    Replies: 6
    Last Post: May 12th, 2008, 01:19 PM

Tags for this Thread

Bookmarks

Posting Permissions

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

SEO by vBSEO