code_junkie

How do I disable all controls in ASP.NET page

Nov 14th, 2011
188
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.23 KB | None | 0 0
  1. foreach (Control c in this.Page.Controls)
  2. {
  3. if (c is DropDownList)
  4. ((DropDownList)(c)).Enabled = false;
  5. }
  6.  
  7. protected void DisableControls(Control parent) {
  8. foreach(Control c in parent.Controls) {
  9. if (c is DropDownList) {
  10. ((DropDownList)(c)).Enabled = false;
  11. }
  12.  
  13. DisableControls(c);
  14. }
  15. }
  16.  
  17. protected void Event_Name(...) {
  18. DisableControls(Page); // use whatever top-most control has all the dropdowns or just the page control
  19. }
  20.  
  21. For Each c As Control In parentControl.Controls
  22. If TypeOf (c) Is CheckBox Then
  23. CType(c, CheckBox).Enabled = enable
  24. ElseIf TypeOf (c) Is RadioButtonList Then
  25. CType(c, RadioButtonList).Enabled = enable
  26. End If
  27. SetControls(c)
  28. Next
  29.  
  30. End Sub
  31.  
  32. protected void Page_Load(object sender, EventArgs e)
  33. {
  34. DisableChilds(this.Page);
  35. }
  36.  
  37. private void DisableChilds(Control ctrl)
  38. {
  39. foreach (Control c in ctrl.Controls)
  40. {
  41. DisableChilds(c);
  42. if (c is DropDownList)
  43. {
  44. ((DropDownList)(c)).Enabled = false;
  45. }
  46. }
  47. }
  48.  
  49. <body>
  50. <form id="form1" runat="server">
  51. ...
  52. </form>
  53. </body>
  54.  
  55. protected void Page_Load(object sender, EventArgs e)
  56. {
  57. form1.Disabled = true;
  58. }
Add Comment
Please, Sign In to add comment