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?
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?
I use this to reset my forms. You should be able to modify it to do as you need:
To call, do this: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
OR:Code:ResetForm(Page.Controls)
The second example only gets the control collection of the Panel "pnlContainer" and leaves the rest on the page alone.Code:ResetForm(pnlContainer.Controls)
jmurrayhead
If you agree, give me rep.
If you like it here...throw us a few bones to help support us.
Thanks J.
I'd sort of come up with this, so far, but it's not disabling the textboxes.
I presume it doesn't make any difference that my Master Page contains the form tag etc., does it?Code:foreach (Control ctrl in Form.Controls) { TextBox txtbox; if(ctrl.GetType()==typeof(TextBox)) { txtbox = (TextBox)ctrl; txtbox.Enabled = false; } }
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.
Same. You can still edit the text in the textboxes.
OK. I added:-
in the if condition and it's this that's failing. So I never get an instance ofCode:txtbox.Text = "This Text";
Strange...Code:if(ctrl.GetType()==typeof(TextBox))
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.
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);
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?
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.
Bookmarks