Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > ASP .Net > ASP .Net Web Controls > ASP.NET Readonly panel function with readonly checkbox, readonly radiobutton

Reply
Thread Tools

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

 
 
Jonathan Hyatt
Guest
Posts: n/a
 
      05-10-2004
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;
}
}
}
 
Reply With Quote
 
 
 
 
Jonathan Hyatt
Guest
Posts: n/a
 
      06-08-2004
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);

}
}
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
RadioButton ReadOnly =?Utf-8?B?QWxoYW1icmEgRWlkb3MgS2lxdWVuZXQ=?= ASP .Net 1 05-24-2007 03:42 PM
Re: Making RadioButton Server control Readonly anat.avidov@gmail.com ASP .Net 0 12-19-2004 01:40 PM
Delete a panel inside a panel HS1 Java 3 12-07-2004 10:50 PM
A readonly panel? guy ASP .Net Web Controls 1 05-04-2004 01:17 AM
Email From Forms with asp:panel.(s) Only one panel works? Harry ASP .Net 3 11-10-2003 03:13 AM



Advertisments