Solved

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

Posted on 2009-07-04
6
718 Views
Last Modified: 2012-05-07
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?
0
Comment
Question by:meetpd
  • 3
  • 2
6 Comments
 
LVL 1

Assisted Solution

by:JaressLoo
JaressLoo earned 200 total points
ID: 24779016
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
 

Author Comment

by:meetpd
ID: 24779025
Thanks for the quick reply.

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

0
 
LVL 1

Expert Comment

by:JaressLoo
ID: 24779055
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
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 

Author Comment

by:meetpd
ID: 24779582
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
 
LVL 5

Accepted Solution

by:
Buginator earned 300 total points
ID: 24780007
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
 
LVL 1

Expert Comment

by:JaressLoo
ID: 24780137
You learn something new every day! :)
0

Featured Post

Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

Join & Write a Comment

ASP.Net to Oracle Connectivity Recently I had to develop an ASP.NET application connecting to an Oracle database.As I am doing it first time ,I had to solve several problems. This article will help to such developers  to develop an ASP.NET client…
User art_snob (http://www.experts-exchange.com/M_6114203.html) encountered strange behavior of Android Web browser on his Mobile Web site. It took a while to find the true cause. It happens so, that the Android Web browser (at least up to OS ver. 2.…
Illustrator's Shape Builder tool will let you combine shapes visually and interactively. This video shows the Mac version, but the tool works the same way in Windows. To follow along with this video, you can draw your own shapes or download the file…
This video demonstrates how to create an example email signature rule for a department in a company using CodeTwo Exchange Rules. The signature will be inserted beneath users' latest emails in conversations and will be displayed in users' Sent Items…

747 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now