How to enable / disable ALL controls (textboxes, dropdowns, checkboxes) on a page (on page load) based on some values in database?

Hi,

I want to disable (make it read only)
all user input controls (like textboxes, dropdowns, checkboxes, radiobuttons)
on a page (on page load) based on some value in the database.

How do I achieve this?
meetpdAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

JaressLooCommented:
I usually do this by creating some sort of method called "ResetFields()" and then call it in the Page_Load function.

Make sure to check if the page is posted back first:

If (!IsPostBack) {
    ResetFields();
}

As far as getting the value from the database, you can do something like the attached code snippet.

I wrote this from the top of my head just in the text box...so I had no error checking...
using System.Data.SqlClient;
 
String connStr = ConfigurationManager.ConnectionStrings("ConnectionStringName").ToString();
 
SQLConnection sqlcon = New SQLConnection;
SQLCommand SQLcom = new SQLCommand;
sqlcon.ConnectionString = connStr;
sqlcom.CommandText = "SELECT Whatever FROM Table";
sqlcom.Connection = sqlcon;
sqlcom.Connection.Open();
String result = sqlcom.ExecuteScalar();
sqlcom.Connection.Close();

Open in new window

0
meetpdAuthor Commented:
Thanks for the quick reply.

Can you please tell me which line actually does the disabling part?

0
JaressLooCommented:
YourControlName.Enabled = False;

Sorry. I didn't put it. That goes in ResetFields() routine.

I'm on my iPhone right now. Sorry for the brief response.
0
Become a CompTIA Certified Healthcare IT Tech

This course will help prep you to earn the CompTIA Healthcare IT Technician certification showing that you have the knowledge and skills needed to succeed in installing, managing, and troubleshooting IT systems in medical and clinical settings.

meetpdAuthor Commented:
The way you suggested, we will have write command for each and every control..I want a way by which ALL controls are disabled in one shot...
0
BuginatorCommented:
Hi,

here you go. I put most of the controls, but add controls as you like (as long as they have the "Enabled" property).


//Run method:
DisableAllControls(Page);
 
//Code for method
    private void DisableAllControls(Control Page)
    {
        foreach (Control ctrl in Page.Controls)
        {
            if (ctrl is TextBox) ((TextBox) (ctrl)).Enabled = false;
            else if (ctrl is Button) ((Button)(ctrl)).Enabled = false;
            else if (ctrl is DropDownList) ((DropDownList)(ctrl)).Enabled = false;
            else if (ctrl is ListBox) ((ListBox)(ctrl)).Enabled = false;
            else if (ctrl is CheckBox) ((CheckBox)(ctrl)).Enabled = false;
            else if (ctrl is CheckBoxList) ((CheckBoxList)(ctrl)).Enabled = false;
            else if (ctrl is RadioButton) ((RadioButton)(ctrl)).Enabled = false;
            else if (ctrl is RadioButtonList) ((RadioButtonList)(ctrl)).Enabled = false;
 
            else
            {
                if (ctrl.Controls.Count > 0) DisableAllControls(ctrl);
            }
        }
    }

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
JaressLooCommented:
You learn something new every day! :)
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
ASP.NET

From novice to tech pro — start learning today.