ASP.NET Readonly panel function with readonly checkbox, readonly radiobutton

J

Jonathan Hyatt

I was recently looking for a way to make multiple controls "read-only"
without graying them out and found a variety of methods. I combined
them to create something that worked for me and wanted to post it
here.

See the function "MakeReadOnly" below.

It is a function that takes a control and makes controls of type
Textbox, Checkbox and RadioButton read-only. You can use similar
methods for other controls. If anyone else has an easier way to do
this, let me know!


Thanks.
Jonathan Hyatt
Contact me here:
http://www.jonhyatt.com/Subscribe.aspx


/// <summary>
/// Cycles through all controls contained in a WebControl and sets
them
/// to "ReadOnly" based on their Type. "ReadOnly" changes from
/// control to control since TextBoxes are the only type that actually
has
/// a built-in readonly attribute.
/// The controls still appear "white" and not "grayed out" as if they
had
/// the enabled attribute set to false
/// </summary>
/// <param name="parentControl">Parent Control that contains controls
to be turned ReadOnly</param>
public void MakeReadOnly(Control parentControl)
{
foreach (Control ctrl in parentControl.Controls)
{
switch (ctrl.GetType().Name)
{
case "TextBox":
((TextBox)ctrl).ReadOnly = true;
((TextBox)ctrl).TabIndex = -1;
break;

case "CheckBox":
CheckBox ckCtrl = (CheckBox)ctrl;
string sChecked=ckCtrl.Checked.ToString().ToLower();
ckCtrl.Attributes.Add("onClick","javascript: this.checked=" +
sChecked + ";");
ckCtrl.TabIndex=-1;
break;

case "RadioButton":
RadioButton rbCtrl = (RadioButton)ctrl;
string sRBChecked=rbCtrl.Checked.ToString().ToLower();
rbCtrl.GroupName=String.Empty; //Eliminate association with other
RadioButtons so that they don't automatically turn off
rbCtrl.Attributes.Add("onClick","javascript: this.checked=" +
sRBChecked + ";");
break;
}
}
}
 
J

Jonathan Hyatt

same code as above with a couple of enhancements:
Added Dropdownlists and recursion to do child controls (as you might
find in a datagrid with controls)



/// <summary>
/// Cycles through all controls contained in a WebControl and sets
them
/// to "ReadOnly" based on their Type. "ReadOnly" changes from
/// control to control since TextBoxes are the only type that actually
has
/// a built-in readonly attribute.
/// The controls still appear "white" and not "grayed out" as if they
had
/// the enabled attribute set to false
/// Recursively calls as well for any child controls, so such things
like
/// datagrids with controls work.
/// </summary>
/// <param name="parentControl">Parent Control that contains controls
to be turned ReadOnly</param>
public void MakeReadOnly(Control parentControl)
{
foreach (Control ctrl in parentControl.Controls)
{
switch (ctrl.GetType().Name)
{
case "TextBox":
((TextBox)ctrl).ReadOnly = true;
((TextBox)ctrl).TabIndex = -1;
break;

case "CheckBox":
CheckBox ckCtrl = (CheckBox)ctrl;
string sChecked=ckCtrl.Checked.ToString().ToLower();
ckCtrl.Attributes.Add("onClick","javascript: this.checked=" + sChecked
+ ";");
ckCtrl.TabIndex=-1;
break;

case "RadioButton":
RadioButton rbCtrl = (RadioButton)ctrl;
string sRBChecked=rbCtrl.Checked.ToString().ToLower();
rbCtrl.GroupName=String.Empty; //Eliminate association with other
RadioButtons so that they don't automatically turn off
rbCtrl.Attributes.Add("onClick","javascript: this.checked=" +
sRBChecked + ";");
break;

case "DropDownList":
DropDownList dd = ((DropDownList)ctrl);
dd.Attributes.Add("onChange","javascript:
this.selectedIndex='"+dd.SelectedIndex.ToString()+"';");
break;
}
// Recursively do any child controls as well.
foreach (Control ctrlChild in ctrl.Controls)
MakeReadOnly(ctrlChild);

}
}
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,995
Messages
2,570,233
Members
46,820
Latest member
GilbertoA5

Latest Threads

Top