Disable all form controls

Discussion in 'ASP .Net Web Controls' started by Evan M., Aug 16, 2005.

  1. Evan M.

    Evan M. Guest

    Hello

    I'm trying to disable all the WebControls in my form under condition
    (i.e. data is missing). So I'm trying the following lines:

    foreach (System.Web.UI.WebControls.WebControl c in
    EditingForm.Controls)
    c.Enabled = false;

    However, when I try and hit the page, I get this exception:
    System.InvalidCastException: Specified cast is not valid.

    So if this refuses to work, who can tell me how I can disable all the
    web controls on my page without having to explicitly hit each one?
     
    Evan M., Aug 16, 2005
    #1
    1. Advertising

  2. Evan M.

    Hibri Guest

    Hi,
    Check if the control is a webcontrol before you disable it
    like;

    foreach (System.Web.UI.Control c in
    EditingForm.Controls){
    if(c.getType()== typeof(System.Web.UI.WebControls.WebControl){
    c.Enabled = false;
    }
    }

    Not all controls in the control collection will be of the type WebControl.
    HTH


    On Tue, 16 Aug 2005 18:58:45 +0100, Evan M. <>
    wrote:

    > Hello
    >
    > I'm trying to disable all the WebControls in my form under condition
    > (i.e. data is missing). So I'm trying the following lines:
    >
    > foreach (System.Web.UI.WebControls.WebControl c in
    > EditingForm.Controls)
    > c.Enabled = false;
    >
    > However, when I try and hit the page, I get this exception:
    > System.InvalidCastException: Specified cast is not valid.
    >
    > So if this refuses to work, who can tell me how I can disable all the
    > web controls on my page without having to explicitly hit each one?
    >




    --
    ..Hibri
    www.hibri.net
     
    Hibri, Aug 18, 2005
    #2
    1. Advertising

  3. Evan M.

    Evan M. Guest

    Thanks. Discovered there were LiteralControls intermixed with the
    WebControls for some reason :p

    Works perfectly now.
     
    Evan M., Aug 18, 2005
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Edwin Knoppert

    Disable all controls in...

    Edwin Knoppert, Jan 22, 2006, in forum: ASP .Net
    Replies:
    6
    Views:
    10,976
    dayalaman
    Dec 4, 2007
  2. Replies:
    4
    Views:
    11,669
  3. Bart Van der Donck

    disable all elements from all forms except <textarea>'s

    Bart Van der Donck, Feb 1, 2005, in forum: Javascript
    Replies:
    3
    Views:
    125
    Stephen Chalmers
    Feb 1, 2005
  4. Replies:
    6
    Views:
    153
  5. Replies:
    4
    Views:
    207
Loading...

Share This Page