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;
}
}
}
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;
}
}
}