0

I found these instructions here in the stack overflow somewhere ... I only added the last loop to find the only objects that interest me i.e. textbox and dropdownlist ... For found objects I need to change the ReadOnly property from true to false or vice versa ... I'm not able to identify how to I can define the object name of the affected objects ...

Thanks in advance for your help.

        foreach (Control ctlMaster in Page.Controls)
        {
            if (ctlMaster is MasterPage)
            {
                foreach (Control ctlForm in ctlMaster.Controls)
                {
                    if (ctlForm is HtmlForm)
                    {
                        foreach (Control ctlContent in ctlForm.Controls)
                        {
                            if (ctlContent is ContentPlaceHolder)
                            {
                                foreach (Control ctlChild in ctlContent.Controls)
                                {
                                    if (ctlChild is Panel)
                                    {
                                        foreach (Control ctlform in ctlChild.Controls)
                                        {
                                            if (!string.IsNullOrEmpty(ctlform.ID))
                                            {
                                                Debug.WriteLine("ID = [" + ctlform.ID + "]");
                                                Debug.WriteLine("UniqueID = [" + ctlform.UniqueID + "]");
                                                Debug.WriteLine("type = [" + ctlform.GetType() + "]");
                                                if(ctlform.GetType().ToString().IndexOf("TextBox") != 0
                                                || ctlform.GetType().ToString().IndexOf("DropDownList") != 0)
                                                {
                                                    // **??? objectName.ReadOnly = true; ???**
                                                }
                                            }
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
            }
        }

1 Answers1

0

is can accept an additional argument after the type name that creates a variable of the type you inspected:

if(ctlform is TextBox t)
  t.ReadOnly = ...

If you're using an old version of c# that doesn't have it you can use as or perform a cast:

if(ctlform is TextBox)
  (ctlform as TextBox).ReadOnly = ...

if(ctlform is TextBox)
  ((TextBox)ctlform).ReadOnly = ...

DropDownList doesn't have a ReadOnly property; perhaps you'll have to use Enabled?

Caius Jard
  • 72,509
  • 5
  • 49
  • 80