Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. Web Development
  3. ASP.NET
  4. Disable all textboxes in a page.

Disable all textboxes in a page.

Scheduled Pinned Locked Moved ASP.NET
helpdesignquestion
7 Posts 6 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • M Offline
    M Offline
    MY1201
    wrote on last edited by
    #1

    Hello. I have a problem - and it's probably me not understanding the concept - but I want to programatically disable all controls in my aspx page - if the user watching the page is not authorized to edit the content. Now this should be an easy task, so I've tried a couple of methods like looping through the page's Controls property and have all textboxes set their Enabled property to false. The following code does not work. I'm using a MasterPage, but I can't think of anything, that makes this a problem. When I "ask" the MasterPage to print out the "count" for it's controls property it says there is about 5 items in the list. Although the page contains around 10 TextBoxes! So what am I misunderstanding here? Anyway here's the code:

    foreach (Control c in Page.Controls)
    {
    switch (c.GetType().ToString())
    {
    case "System.Web.UI.WebControls.TextBox":
    ((TextBox)c).Enabled = false;
    break;
    case "System.Web.UI.WebControls.DropDownList":
    ((DropDownList)c).Enabled = false;
    break;
    case "System.Web.UI.WebControls.ImageButton":
    ((ImageButton)c).Enabled = false;
    break;
    default:
    break;
    }
    }

    I hope someone can help! Thanks in advance. Best Regards Soeren

    M H A B 4 Replies Last reply
    0
    • M MY1201

      Hello. I have a problem - and it's probably me not understanding the concept - but I want to programatically disable all controls in my aspx page - if the user watching the page is not authorized to edit the content. Now this should be an easy task, so I've tried a couple of methods like looping through the page's Controls property and have all textboxes set their Enabled property to false. The following code does not work. I'm using a MasterPage, but I can't think of anything, that makes this a problem. When I "ask" the MasterPage to print out the "count" for it's controls property it says there is about 5 items in the list. Although the page contains around 10 TextBoxes! So what am I misunderstanding here? Anyway here's the code:

      foreach (Control c in Page.Controls)
      {
      switch (c.GetType().ToString())
      {
      case "System.Web.UI.WebControls.TextBox":
      ((TextBox)c).Enabled = false;
      break;
      case "System.Web.UI.WebControls.DropDownList":
      ((DropDownList)c).Enabled = false;
      break;
      case "System.Web.UI.WebControls.ImageButton":
      ((ImageButton)c).Enabled = false;
      break;
      default:
      break;
      }
      }

      I hope someone can help! Thanks in advance. Best Regards Soeren

      M Offline
      M Offline
      Michael Sync
      wrote on last edited by
      #2

      The easiest way to do is showing "50% transparency image" with higher z-order. All you need to do is that you hav to create one gif image with transparency background. (file size should not be too big.) set the position of that image to absolute. and set the width and height of this image to 100%.. finally, set the zorder of image to highest value..(you may check out "LightBox" javascript toolkit.) hopefully, you got my point. :)

      Thanks and Regards, Michael Sync ( Blog: http://michaelsync.net) If you want to thank me for my help, please vote my message. Thank you.

      1 Reply Last reply
      0
      • M MY1201

        Hello. I have a problem - and it's probably me not understanding the concept - but I want to programatically disable all controls in my aspx page - if the user watching the page is not authorized to edit the content. Now this should be an easy task, so I've tried a couple of methods like looping through the page's Controls property and have all textboxes set their Enabled property to false. The following code does not work. I'm using a MasterPage, but I can't think of anything, that makes this a problem. When I "ask" the MasterPage to print out the "count" for it's controls property it says there is about 5 items in the list. Although the page contains around 10 TextBoxes! So what am I misunderstanding here? Anyway here's the code:

        foreach (Control c in Page.Controls)
        {
        switch (c.GetType().ToString())
        {
        case "System.Web.UI.WebControls.TextBox":
        ((TextBox)c).Enabled = false;
        break;
        case "System.Web.UI.WebControls.DropDownList":
        ((DropDownList)c).Enabled = false;
        break;
        case "System.Web.UI.WebControls.ImageButton":
        ((ImageButton)c).Enabled = false;
        break;
        default:
        break;
        }
        }

        I hope someone can help! Thanks in advance. Best Regards Soeren

        H Offline
        H Offline
        Harini N K
        wrote on last edited by
        #3

        Hi I think better way for disabling fields is to write javascript function. I have added sample code below. Hope this helps. 'aspnetForm' - Form name of a web form
        Example:
        function DisableFields(vAccessRights) { //parameter - vAccessRights - to check access rights for particular user.. elm=document.aspnetForm.elements; for(i=0; i < elm.length ; i++) { if(elm[i].type=="checkbox") { var chkboxId = elm[i].id; //add code to enable / disable field } else if(elm[i].type=="textbox") { var txtboxId = elm[i].id; //add code to enable / disable field here } } } - Harini

        1 Reply Last reply
        0
        • M MY1201

          Hello. I have a problem - and it's probably me not understanding the concept - but I want to programatically disable all controls in my aspx page - if the user watching the page is not authorized to edit the content. Now this should be an easy task, so I've tried a couple of methods like looping through the page's Controls property and have all textboxes set their Enabled property to false. The following code does not work. I'm using a MasterPage, but I can't think of anything, that makes this a problem. When I "ask" the MasterPage to print out the "count" for it's controls property it says there is about 5 items in the list. Although the page contains around 10 TextBoxes! So what am I misunderstanding here? Anyway here's the code:

          foreach (Control c in Page.Controls)
          {
          switch (c.GetType().ToString())
          {
          case "System.Web.UI.WebControls.TextBox":
          ((TextBox)c).Enabled = false;
          break;
          case "System.Web.UI.WebControls.DropDownList":
          ((DropDownList)c).Enabled = false;
          break;
          case "System.Web.UI.WebControls.ImageButton":
          ((ImageButton)c).Enabled = false;
          break;
          default:
          break;
          }
          }

          I hope someone can help! Thanks in advance. Best Regards Soeren

          A Offline
          A Offline
          anufabian
          wrote on last edited by
          #4

          I had some what the same problem . Finally I had put the controls in a panel and do something like this Dim ctrls As Control For Each ctrls In Me.Panel1.Controls If TypeOf ctrls Is TextBox Then Dim tb As TextBox tb = ctrls tb.Text = DateTime.Today tb.Enabled = False (instead u can manipulate any property here) End If Next

          S 1 Reply Last reply
          0
          • A anufabian

            I had some what the same problem . Finally I had put the controls in a panel and do something like this Dim ctrls As Control For Each ctrls In Me.Panel1.Controls If TypeOf ctrls Is TextBox Then Dim tb As TextBox tb = ctrls tb.Text = DateTime.Today tb.Enabled = False (instead u can manipulate any property here) End If Next

            S Offline
            S Offline
            Sandeep Akhare
            wrote on last edited by
            #5

            Use PlaceHoldet instead of panel :)

            Thanks and Regards Sandeep

            1 Reply Last reply
            0
            • M MY1201

              Hello. I have a problem - and it's probably me not understanding the concept - but I want to programatically disable all controls in my aspx page - if the user watching the page is not authorized to edit the content. Now this should be an easy task, so I've tried a couple of methods like looping through the page's Controls property and have all textboxes set their Enabled property to false. The following code does not work. I'm using a MasterPage, but I can't think of anything, that makes this a problem. When I "ask" the MasterPage to print out the "count" for it's controls property it says there is about 5 items in the list. Although the page contains around 10 TextBoxes! So what am I misunderstanding here? Anyway here's the code:

              foreach (Control c in Page.Controls)
              {
              switch (c.GetType().ToString())
              {
              case "System.Web.UI.WebControls.TextBox":
              ((TextBox)c).Enabled = false;
              break;
              case "System.Web.UI.WebControls.DropDownList":
              ((DropDownList)c).Enabled = false;
              break;
              case "System.Web.UI.WebControls.ImageButton":
              ((ImageButton)c).Enabled = false;
              break;
              default:
              break;
              }
              }

              I hope someone can help! Thanks in advance. Best Regards Soeren

              B Offline
              B Offline
              badgrs
              wrote on last edited by
              #6

              All the ansers above have severe flaws. Doing it client side is a VERY bad idea, especially if security if the reason for disabling them in the first place. Your code is pretty much correct, the problem is that controls can contain other controls so your foreach loop will only disable the top-level ones. You need to put that in a function that recursevly calls itself if the control has children. Heres one I use:

              private void DisableInputs(Control control)
                  {
                      foreach (Control c in control.Controls)
                      {
                          if (typeof(HtmlControl).IsInstanceOfType(c))
                          {
                              ((HtmlControl)c).Disabled = true;
                          }
                          else if (typeof(WebControl).IsInstanceOfType(c))
                          {
                              ((WebControl)c).Enabled = false;
                          }
                          DisableInputs(c);
                      }
                  }
              
              B 1 Reply Last reply
              0
              • B badgrs

                All the ansers above have severe flaws. Doing it client side is a VERY bad idea, especially if security if the reason for disabling them in the first place. Your code is pretty much correct, the problem is that controls can contain other controls so your foreach loop will only disable the top-level ones. You need to put that in a function that recursevly calls itself if the control has children. Heres one I use:

                private void DisableInputs(Control control)
                    {
                        foreach (Control c in control.Controls)
                        {
                            if (typeof(HtmlControl).IsInstanceOfType(c))
                            {
                                ((HtmlControl)c).Disabled = true;
                            }
                            else if (typeof(WebControl).IsInstanceOfType(c))
                            {
                                ((WebControl)c).Enabled = false;
                            }
                            DisableInputs(c);
                        }
                    }
                
                B Offline
                B Offline
                badgrs
                wrote on last edited by
                #7

                As a follow-up to what I said about security - even setting the disabled attribute server-side won't stop the user being able to submit changed data, there is always a way (eg Firebug). You'll also want to perform some sort of authenitcation check in whatever code handles the posted data.

                1 Reply Last reply
                0
                Reply
                • Reply as topic
                Log in to reply
                • Oldest to Newest
                • Newest to Oldest
                • Most Votes


                • Login

                • Don't have an account? Register

                • Login or register to search.
                • First post
                  Last post
                0
                • Categories
                • Recent
                • Tags
                • Popular
                • World
                • Users
                • Groups