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

Discussion in 'ASP .Net Web Controls' started by Jonathan Hyatt, May 10, 2004.

  1. 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;
    }
    }
    }
     
    Jonathan Hyatt, May 10, 2004
    #1
    1. Advertising

  2. 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);

    }
    }
     
    Jonathan Hyatt, Jun 8, 2004
    #2
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. bruce barker

    Re: Checkbox and Radiobutton onfocus events

    bruce barker, Jul 23, 2003, in forum: ASP .Net
    Replies:
    0
    Views:
    1,213
    bruce barker
    Jul 23, 2003
  2. alien2_51
    Replies:
    2
    Views:
    3,972
  3. Abhishek Srivastava

    Assign ID to a radiobutton inside a checkbox

    Abhishek Srivastava, Feb 5, 2004, in forum: ASP .Net
    Replies:
    2
    Views:
    1,066
    Alvin Bruney [MVP]
    Mar 2, 2004
  4. Replies:
    0
    Views:
    3,674
  5. =?Utf-8?B?QWxoYW1icmEgRWlkb3MgS2lxdWVuZXQ=?=

    RadioButton ReadOnly

    =?Utf-8?B?QWxoYW1icmEgRWlkb3MgS2lxdWVuZXQ=?=, May 24, 2007, in forum: ASP .Net
    Replies:
    1
    Views:
    805
    sloan
    May 24, 2007
Loading...

Share This Page